The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 19 | import android.content.ClipData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 21 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Bitmap; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 25 | import android.graphics.Camera; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 27 | import android.graphics.Interpolator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.graphics.LinearGradient; |
| 29 | import android.graphics.Matrix; |
| 30 | import android.graphics.Paint; |
| 31 | import android.graphics.PixelFormat; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 32 | import android.graphics.Point; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.graphics.PorterDuff; |
| 34 | import android.graphics.PorterDuffXfermode; |
| 35 | import android.graphics.Rect; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 36 | import android.graphics.RectF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.graphics.Region; |
| 38 | import android.graphics.Shader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.graphics.drawable.ColorDrawable; |
| 40 | import android.graphics.drawable.Drawable; |
| 41 | import android.os.Handler; |
| 42 | import android.os.IBinder; |
| 43 | import android.os.Message; |
| 44 | import android.os.Parcel; |
| 45 | import android.os.Parcelable; |
| 46 | import android.os.RemoteException; |
| 47 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.util.AttributeSet; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 49 | import android.util.FloatProperty; |
| 50 | import android.util.LocaleUtil; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.util.Log; |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 52 | import android.util.Pool; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 53 | import android.util.Poolable; |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 54 | import android.util.PoolableManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 55 | import android.util.Pools; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 56 | import android.util.Property; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.util.SparseArray; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 58 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.ContextMenu.ContextMenuInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 60 | import android.view.accessibility.AccessibilityEvent; |
| 61 | import android.view.accessibility.AccessibilityEventSource; |
| 62 | import android.view.accessibility.AccessibilityManager; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 63 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.view.animation.Animation; |
Mike Cleron | 3ecd58c | 2009-09-28 11:39:02 -0700 | [diff] [blame] | 65 | import android.view.animation.AnimationUtils; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 66 | import android.view.inputmethod.EditorInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.view.inputmethod.InputConnection; |
| 68 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.widget.ScrollBarDrawable; |
| 70 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 71 | import com.android.internal.R; |
| 72 | import com.android.internal.util.Predicate; |
| 73 | import com.android.internal.view.menu.MenuBuilder; |
| 74 | |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 75 | import java.lang.ref.WeakReference; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 76 | import java.lang.reflect.InvocationTargetException; |
| 77 | import java.lang.reflect.Method; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import java.util.ArrayList; |
| 79 | import java.util.Arrays; |
Fabrice Di Meglio | 26e432d | 2011-06-10 14:19:18 -0700 | [diff] [blame] | 80 | import java.util.Locale; |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 81 | import java.util.WeakHashMap; |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 82 | import java.util.concurrent.CopyOnWriteArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * <p> |
| 86 | * This class represents the basic building block for user interface components. A View |
| 87 | * occupies a rectangular area on the screen and is responsible for drawing and |
| 88 | * event handling. View is the base class for <em>widgets</em>, which are |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 89 | * used to create interactive UI components (buttons, text fields, etc.). The |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which |
| 91 | * are invisible containers that hold other Views (or other ViewGroups) and define |
| 92 | * their layout properties. |
| 93 | * </p> |
| 94 | * |
| 95 | * <div class="special"> |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 96 | * <p>For an introduction to using this class to develop your |
| 97 | * application's user interface, read the Developer Guide documentation on |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 99 | * include: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a> |
| 101 | * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a> |
| 102 | * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a> |
| 103 | * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a> |
| 104 | * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a> |
| 105 | * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a> |
| 106 | * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a> |
| 107 | * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>. |
| 108 | * </p> |
| 109 | * </div> |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 110 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * <a name="Using"></a> |
| 112 | * <h3>Using Views</h3> |
| 113 | * <p> |
| 114 | * All of the views in a window are arranged in a single tree. You can add views |
| 115 | * either from code or by specifying a tree of views in one or more XML layout |
| 116 | * files. There are many specialized subclasses of views that act as controls or |
| 117 | * are capable of displaying text, images, or other content. |
| 118 | * </p> |
| 119 | * <p> |
| 120 | * Once you have created a tree of views, there are typically a few types of |
| 121 | * common operations you may wish to perform: |
| 122 | * <ul> |
| 123 | * <li><strong>Set properties:</strong> for example setting the text of a |
| 124 | * {@link android.widget.TextView}. The available properties and the methods |
| 125 | * that set them will vary among the different subclasses of views. Note that |
| 126 | * properties that are known at build time can be set in the XML layout |
| 127 | * files.</li> |
| 128 | * <li><strong>Set focus:</strong> The framework will handled moving focus in |
| 129 | * response to user input. To force focus to a specific view, call |
| 130 | * {@link #requestFocus}.</li> |
| 131 | * <li><strong>Set up listeners:</strong> Views allow clients to set listeners |
| 132 | * that will be notified when something interesting happens to the view. For |
| 133 | * example, all views will let you set a listener to be notified when the view |
| 134 | * gains or loses focus. You can register such a listener using |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 135 | * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}. |
| 136 | * Other view subclasses offer more specialized listeners. For example, a Button |
| 137 | * exposes a listener to notify clients when the button is clicked.</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * <li><strong>Set visibility:</strong> You can hide or show views using |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 139 | * {@link #setVisibility(int)}.</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | * </ul> |
| 141 | * </p> |
| 142 | * <p><em> |
| 143 | * Note: The Android framework is responsible for measuring, laying out and |
| 144 | * drawing views. You should not call methods that perform these actions on |
| 145 | * views yourself unless you are actually implementing a |
| 146 | * {@link android.view.ViewGroup}. |
| 147 | * </em></p> |
| 148 | * |
| 149 | * <a name="Lifecycle"></a> |
| 150 | * <h3>Implementing a Custom View</h3> |
| 151 | * |
| 152 | * <p> |
| 153 | * To implement a custom view, you will usually begin by providing overrides for |
| 154 | * some of the standard methods that the framework calls on all views. You do |
| 155 | * not need to override all of these methods. In fact, you can start by just |
| 156 | * overriding {@link #onDraw(android.graphics.Canvas)}. |
| 157 | * <table border="2" width="85%" align="center" cellpadding="5"> |
| 158 | * <thead> |
| 159 | * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr> |
| 160 | * </thead> |
| 161 | * |
| 162 | * <tbody> |
| 163 | * <tr> |
| 164 | * <td rowspan="2">Creation</td> |
| 165 | * <td>Constructors</td> |
| 166 | * <td>There is a form of the constructor that are called when the view |
| 167 | * is created from code and a form that is called when the view is |
| 168 | * inflated from a layout file. The second form should parse and apply |
| 169 | * any attributes defined in the layout file. |
| 170 | * </td> |
| 171 | * </tr> |
| 172 | * <tr> |
| 173 | * <td><code>{@link #onFinishInflate()}</code></td> |
| 174 | * <td>Called after a view and all of its children has been inflated |
| 175 | * from XML.</td> |
| 176 | * </tr> |
| 177 | * |
| 178 | * <tr> |
| 179 | * <td rowspan="3">Layout</td> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 180 | * <td><code>{@link #onMeasure(int, int)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | * <td>Called to determine the size requirements for this view and all |
| 182 | * of its children. |
| 183 | * </td> |
| 184 | * </tr> |
| 185 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 186 | * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | * <td>Called when this view should assign a size and position to all |
| 188 | * of its children. |
| 189 | * </td> |
| 190 | * </tr> |
| 191 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 192 | * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * <td>Called when the size of this view has changed. |
| 194 | * </td> |
| 195 | * </tr> |
| 196 | * |
| 197 | * <tr> |
| 198 | * <td>Drawing</td> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 199 | * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | * <td>Called when the view should render its content. |
| 201 | * </td> |
| 202 | * </tr> |
| 203 | * |
| 204 | * <tr> |
| 205 | * <td rowspan="4">Event processing</td> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 206 | * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | * <td>Called when a new key event occurs. |
| 208 | * </td> |
| 209 | * </tr> |
| 210 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 211 | * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | * <td>Called when a key up event occurs. |
| 213 | * </td> |
| 214 | * </tr> |
| 215 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 216 | * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * <td>Called when a trackball motion event occurs. |
| 218 | * </td> |
| 219 | * </tr> |
| 220 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 221 | * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | * <td>Called when a touch screen motion event occurs. |
| 223 | * </td> |
| 224 | * </tr> |
| 225 | * |
| 226 | * <tr> |
| 227 | * <td rowspan="2">Focus</td> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 228 | * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | * <td>Called when the view gains or loses focus. |
| 230 | * </td> |
| 231 | * </tr> |
| 232 | * |
| 233 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 234 | * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | * <td>Called when the window containing the view gains or loses focus. |
| 236 | * </td> |
| 237 | * </tr> |
| 238 | * |
| 239 | * <tr> |
| 240 | * <td rowspan="3">Attaching</td> |
| 241 | * <td><code>{@link #onAttachedToWindow()}</code></td> |
| 242 | * <td>Called when the view is attached to a window. |
| 243 | * </td> |
| 244 | * </tr> |
| 245 | * |
| 246 | * <tr> |
| 247 | * <td><code>{@link #onDetachedFromWindow}</code></td> |
| 248 | * <td>Called when the view is detached from its window. |
| 249 | * </td> |
| 250 | * </tr> |
| 251 | * |
| 252 | * <tr> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 253 | * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | * <td>Called when the visibility of the window containing the view |
| 255 | * has changed. |
| 256 | * </td> |
| 257 | * </tr> |
| 258 | * </tbody> |
| 259 | * |
| 260 | * </table> |
| 261 | * </p> |
| 262 | * |
| 263 | * <a name="IDs"></a> |
| 264 | * <h3>IDs</h3> |
| 265 | * Views may have an integer id associated with them. These ids are typically |
| 266 | * assigned in the layout XML files, and are used to find specific views within |
| 267 | * the view tree. A common pattern is to: |
| 268 | * <ul> |
| 269 | * <li>Define a Button in the layout file and assign it a unique ID. |
| 270 | * <pre> |
Gilles Debunne | 0243caf | 2010-08-24 23:06:35 -0700 | [diff] [blame] | 271 | * <Button |
| 272 | * android:id="@+id/my_button" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | * android:layout_width="wrap_content" |
| 274 | * android:layout_height="wrap_content" |
| 275 | * android:text="@string/my_button_text"/> |
| 276 | * </pre></li> |
| 277 | * <li>From the onCreate method of an Activity, find the Button |
| 278 | * <pre class="prettyprint"> |
| 279 | * Button myButton = (Button) findViewById(R.id.my_button); |
| 280 | * </pre></li> |
| 281 | * </ul> |
| 282 | * <p> |
| 283 | * View IDs need not be unique throughout the tree, but it is good practice to |
| 284 | * ensure that they are at least unique within the part of the tree you are |
| 285 | * searching. |
| 286 | * </p> |
| 287 | * |
| 288 | * <a name="Position"></a> |
| 289 | * <h3>Position</h3> |
| 290 | * <p> |
| 291 | * The geometry of a view is that of a rectangle. A view has a location, |
| 292 | * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and |
| 293 | * two dimensions, expressed as a width and a height. The unit for location |
| 294 | * and dimensions is the pixel. |
| 295 | * </p> |
| 296 | * |
| 297 | * <p> |
| 298 | * It is possible to retrieve the location of a view by invoking the methods |
| 299 | * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X, |
| 300 | * coordinate of the rectangle representing the view. The latter returns the |
| 301 | * top, or Y, coordinate of the rectangle representing the view. These methods |
| 302 | * both return the location of the view relative to its parent. For instance, |
| 303 | * when getLeft() returns 20, that means the view is located 20 pixels to the |
| 304 | * right of the left edge of its direct parent. |
| 305 | * </p> |
| 306 | * |
| 307 | * <p> |
| 308 | * In addition, several convenience methods are offered to avoid unnecessary |
| 309 | * computations, namely {@link #getRight()} and {@link #getBottom()}. |
| 310 | * These methods return the coordinates of the right and bottom edges of the |
| 311 | * rectangle representing the view. For instance, calling {@link #getRight()} |
| 312 | * is similar to the following computation: <code>getLeft() + getWidth()</code> |
| 313 | * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.) |
| 314 | * </p> |
| 315 | * |
| 316 | * <a name="SizePaddingMargins"></a> |
| 317 | * <h3>Size, padding and margins</h3> |
| 318 | * <p> |
| 319 | * The size of a view is expressed with a width and a height. A view actually |
| 320 | * possess two pairs of width and height values. |
| 321 | * </p> |
| 322 | * |
| 323 | * <p> |
| 324 | * The first pair is known as <em>measured width</em> and |
| 325 | * <em>measured height</em>. These dimensions define how big a view wants to be |
| 326 | * within its parent (see <a href="#Layout">Layout</a> for more details.) The |
| 327 | * measured dimensions can be obtained by calling {@link #getMeasuredWidth()} |
| 328 | * and {@link #getMeasuredHeight()}. |
| 329 | * </p> |
| 330 | * |
| 331 | * <p> |
| 332 | * The second pair is simply known as <em>width</em> and <em>height</em>, or |
| 333 | * sometimes <em>drawing width</em> and <em>drawing height</em>. These |
| 334 | * dimensions define the actual size of the view on screen, at drawing time and |
| 335 | * after layout. These values may, but do not have to, be different from the |
| 336 | * measured width and height. The width and height can be obtained by calling |
| 337 | * {@link #getWidth()} and {@link #getHeight()}. |
| 338 | * </p> |
| 339 | * |
| 340 | * <p> |
| 341 | * To measure its dimensions, a view takes into account its padding. The padding |
| 342 | * is expressed in pixels for the left, top, right and bottom parts of the view. |
| 343 | * Padding can be used to offset the content of the view by a specific amount of |
| 344 | * pixels. For instance, a left padding of 2 will push the view's content by |
| 345 | * 2 pixels to the right of the left edge. Padding can be set using the |
| 346 | * {@link #setPadding(int, int, int, int)} method and queried by calling |
| 347 | * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 348 | * {@link #getPaddingRight()}, {@link #getPaddingBottom()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * </p> |
| 350 | * |
| 351 | * <p> |
| 352 | * Even though a view can define a padding, it does not provide any support for |
| 353 | * margins. However, view groups provide such a support. Refer to |
| 354 | * {@link android.view.ViewGroup} and |
| 355 | * {@link android.view.ViewGroup.MarginLayoutParams} for further information. |
| 356 | * </p> |
| 357 | * |
| 358 | * <a name="Layout"></a> |
| 359 | * <h3>Layout</h3> |
| 360 | * <p> |
| 361 | * Layout is a two pass process: a measure pass and a layout pass. The measuring |
| 362 | * pass is implemented in {@link #measure(int, int)} and is a top-down traversal |
| 363 | * of the view tree. Each view pushes dimension specifications down the tree |
| 364 | * during the recursion. At the end of the measure pass, every view has stored |
| 365 | * its measurements. The second pass happens in |
| 366 | * {@link #layout(int,int,int,int)} and is also top-down. During |
| 367 | * this pass each parent is responsible for positioning all of its children |
| 368 | * using the sizes computed in the measure pass. |
| 369 | * </p> |
| 370 | * |
| 371 | * <p> |
| 372 | * When a view's measure() method returns, its {@link #getMeasuredWidth()} and |
| 373 | * {@link #getMeasuredHeight()} values must be set, along with those for all of |
| 374 | * that view's descendants. A view's measured width and measured height values |
| 375 | * must respect the constraints imposed by the view's parents. This guarantees |
| 376 | * that at the end of the measure pass, all parents accept all of their |
| 377 | * children's measurements. A parent view may call measure() more than once on |
| 378 | * its children. For example, the parent may measure each child once with |
| 379 | * unspecified dimensions to find out how big they want to be, then call |
| 380 | * measure() on them again with actual numbers if the sum of all the children's |
| 381 | * unconstrained sizes is too big or too small. |
| 382 | * </p> |
| 383 | * |
| 384 | * <p> |
| 385 | * The measure pass uses two classes to communicate dimensions. The |
| 386 | * {@link MeasureSpec} class is used by views to tell their parents how they |
| 387 | * want to be measured and positioned. The base LayoutParams class just |
| 388 | * describes how big the view wants to be for both width and height. For each |
| 389 | * dimension, it can specify one of: |
| 390 | * <ul> |
| 391 | * <li> an exact number |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 392 | * <li>MATCH_PARENT, which means the view wants to be as big as its parent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | * (minus padding) |
| 394 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough to |
| 395 | * enclose its content (plus padding). |
| 396 | * </ul> |
| 397 | * There are subclasses of LayoutParams for different subclasses of ViewGroup. |
| 398 | * For example, AbsoluteLayout has its own subclass of LayoutParams which adds |
| 399 | * an X and Y value. |
| 400 | * </p> |
| 401 | * |
| 402 | * <p> |
| 403 | * MeasureSpecs are used to push requirements down the tree from parent to |
| 404 | * child. A MeasureSpec can be in one of three modes: |
| 405 | * <ul> |
| 406 | * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension |
| 407 | * of a child view. For example, a LinearLayout may call measure() on its child |
| 408 | * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how |
| 409 | * tall the child view wants to be given a width of 240 pixels. |
| 410 | * <li>EXACTLY: This is used by the parent to impose an exact size on the |
| 411 | * child. The child must use this size, and guarantee that all of its |
| 412 | * descendants will fit within this size. |
| 413 | * <li>AT_MOST: This is used by the parent to impose a maximum size on the |
| 414 | * child. The child must gurantee that it and all of its descendants will fit |
| 415 | * within this size. |
| 416 | * </ul> |
| 417 | * </p> |
| 418 | * |
| 419 | * <p> |
| 420 | * To intiate a layout, call {@link #requestLayout}. This method is typically |
| 421 | * called by a view on itself when it believes that is can no longer fit within |
| 422 | * its current bounds. |
| 423 | * </p> |
| 424 | * |
| 425 | * <a name="Drawing"></a> |
| 426 | * <h3>Drawing</h3> |
| 427 | * <p> |
| 428 | * Drawing is handled by walking the tree and rendering each view that |
| 429 | * intersects the the invalid region. Because the tree is traversed in-order, |
| 430 | * this means that parents will draw before (i.e., behind) their children, with |
| 431 | * siblings drawn in the order they appear in the tree. |
| 432 | * If you set a background drawable for a View, then the View will draw it for you |
| 433 | * before calling back to its <code>onDraw()</code> method. |
| 434 | * </p> |
| 435 | * |
| 436 | * <p> |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 437 | * Note that the framework will not draw views that are not in the invalid region. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | * </p> |
| 439 | * |
| 440 | * <p> |
| 441 | * To force a view to draw, call {@link #invalidate()}. |
| 442 | * </p> |
| 443 | * |
| 444 | * <a name="EventHandlingThreading"></a> |
| 445 | * <h3>Event Handling and Threading</h3> |
| 446 | * <p> |
| 447 | * The basic cycle of a view is as follows: |
| 448 | * <ol> |
| 449 | * <li>An event comes in and is dispatched to the appropriate view. The view |
| 450 | * handles the event and notifies any listeners.</li> |
| 451 | * <li>If in the course of processing the event, the view's bounds may need |
| 452 | * to be changed, the view will call {@link #requestLayout()}.</li> |
| 453 | * <li>Similarly, if in the course of processing the event the view's appearance |
| 454 | * may need to be changed, the view will call {@link #invalidate()}.</li> |
| 455 | * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called, |
| 456 | * the framework will take care of measuring, laying out, and drawing the tree |
| 457 | * as appropriate.</li> |
| 458 | * </ol> |
| 459 | * </p> |
| 460 | * |
| 461 | * <p><em>Note: The entire view tree is single threaded. You must always be on |
| 462 | * the UI thread when calling any method on any view.</em> |
| 463 | * If you are doing work on other threads and want to update the state of a view |
| 464 | * from that thread, you should use a {@link Handler}. |
| 465 | * </p> |
| 466 | * |
| 467 | * <a name="FocusHandling"></a> |
| 468 | * <h3>Focus Handling</h3> |
| 469 | * <p> |
| 470 | * The framework will handle routine focus movement in response to user input. |
| 471 | * This includes changing the focus as views are removed or hidden, or as new |
| 472 | * views become available. Views indicate their willingness to take focus |
| 473 | * through the {@link #isFocusable} method. To change whether a view can take |
| 474 | * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below) |
| 475 | * views indicate whether they still would like focus via {@link #isFocusableInTouchMode} |
| 476 | * and can change this via {@link #setFocusableInTouchMode(boolean)}. |
| 477 | * </p> |
| 478 | * <p> |
| 479 | * Focus movement is based on an algorithm which finds the nearest neighbor in a |
| 480 | * given direction. In rare cases, the default algorithm may not match the |
| 481 | * intended behavior of the developer. In these situations, you can provide |
| 482 | * explicit overrides by using these XML attributes in the layout file: |
| 483 | * <pre> |
| 484 | * nextFocusDown |
| 485 | * nextFocusLeft |
| 486 | * nextFocusRight |
| 487 | * nextFocusUp |
| 488 | * </pre> |
| 489 | * </p> |
| 490 | * |
| 491 | * |
| 492 | * <p> |
| 493 | * To get a particular view to take focus, call {@link #requestFocus()}. |
| 494 | * </p> |
| 495 | * |
| 496 | * <a name="TouchMode"></a> |
| 497 | * <h3>Touch Mode</h3> |
| 498 | * <p> |
| 499 | * When a user is navigating a user interface via directional keys such as a D-pad, it is |
| 500 | * necessary to give focus to actionable items such as buttons so the user can see |
| 501 | * what will take input. If the device has touch capabilities, however, and the user |
| 502 | * begins interacting with the interface by touching it, it is no longer necessary to |
| 503 | * always highlight, or give focus to, a particular view. This motivates a mode |
| 504 | * for interaction named 'touch mode'. |
| 505 | * </p> |
| 506 | * <p> |
| 507 | * For a touch capable device, once the user touches the screen, the device |
| 508 | * will enter touch mode. From this point onward, only views for which |
| 509 | * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets. |
| 510 | * Other views that are touchable, like buttons, will not take focus when touched; they will |
| 511 | * only fire the on click listeners. |
| 512 | * </p> |
| 513 | * <p> |
| 514 | * Any time a user hits a directional key, such as a D-pad direction, the view device will |
| 515 | * exit touch mode, and find a view to take focus, so that the user may resume interacting |
| 516 | * with the user interface without touching the screen again. |
| 517 | * </p> |
| 518 | * <p> |
| 519 | * The touch mode state is maintained across {@link android.app.Activity}s. Call |
| 520 | * {@link #isInTouchMode} to see whether the device is currently in touch mode. |
| 521 | * </p> |
| 522 | * |
| 523 | * <a name="Scrolling"></a> |
| 524 | * <h3>Scrolling</h3> |
| 525 | * <p> |
| 526 | * The framework provides basic support for views that wish to internally |
| 527 | * scroll their content. This includes keeping track of the X and Y scroll |
| 528 | * offset as well as mechanisms for drawing scrollbars. See |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 529 | * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 530 | * {@link #awakenScrollBars()} for more details. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | * </p> |
| 532 | * |
| 533 | * <a name="Tags"></a> |
| 534 | * <h3>Tags</h3> |
| 535 | * <p> |
| 536 | * Unlike IDs, tags are not used to identify views. Tags are essentially an |
| 537 | * extra piece of information that can be associated with a view. They are most |
| 538 | * often used as a convenience to store data related to views in the views |
| 539 | * themselves rather than by putting them in a separate structure. |
| 540 | * </p> |
| 541 | * |
| 542 | * <a name="Animation"></a> |
| 543 | * <h3>Animation</h3> |
| 544 | * <p> |
| 545 | * You can attach an {@link Animation} object to a view using |
| 546 | * {@link #setAnimation(Animation)} or |
| 547 | * {@link #startAnimation(Animation)}. The animation can alter the scale, |
| 548 | * rotation, translation and alpha of a view over time. If the animation is |
| 549 | * attached to a view that has children, the animation will affect the entire |
| 550 | * subtree rooted by that node. When an animation is started, the framework will |
| 551 | * take care of redrawing the appropriate views until the animation completes. |
| 552 | * </p> |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 553 | * <p> |
| 554 | * Starting with Android 3.0, the preferred way of animating views is to use the |
| 555 | * {@link android.animation} package APIs. |
| 556 | * </p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | * |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 558 | * <a name="Security"></a> |
| 559 | * <h3>Security</h3> |
| 560 | * <p> |
| 561 | * Sometimes it is essential that an application be able to verify that an action |
| 562 | * is being performed with the full knowledge and consent of the user, such as |
| 563 | * granting a permission request, making a purchase or clicking on an advertisement. |
| 564 | * Unfortunately, a malicious application could try to spoof the user into |
| 565 | * performing these actions, unaware, by concealing the intended purpose of the view. |
| 566 | * As a remedy, the framework offers a touch filtering mechanism that can be used to |
| 567 | * improve the security of views that provide access to sensitive functionality. |
| 568 | * </p><p> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 569 | * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 570 | * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 571 | * will discard touches that are received whenever the view's window is obscured by |
| 572 | * another visible window. As a result, the view will not receive touches whenever a |
| 573 | * toast, dialog or other window appears above the view's window. |
| 574 | * </p><p> |
| 575 | * For more fine-grained control over security, consider overriding the |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 576 | * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own |
| 577 | * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}. |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 578 | * </p> |
| 579 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 580 | * @attr ref android.R.styleable#View_alpha |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 581 | * @attr ref android.R.styleable#View_background |
| 582 | * @attr ref android.R.styleable#View_clickable |
| 583 | * @attr ref android.R.styleable#View_contentDescription |
| 584 | * @attr ref android.R.styleable#View_drawingCacheQuality |
| 585 | * @attr ref android.R.styleable#View_duplicateParentState |
| 586 | * @attr ref android.R.styleable#View_id |
| 587 | * @attr ref android.R.styleable#View_fadingEdge |
| 588 | * @attr ref android.R.styleable#View_fadingEdgeLength |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 589 | * @attr ref android.R.styleable#View_filterTouchesWhenObscured |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | * @attr ref android.R.styleable#View_fitsSystemWindows |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 591 | * @attr ref android.R.styleable#View_isScrollContainer |
| 592 | * @attr ref android.R.styleable#View_focusable |
| 593 | * @attr ref android.R.styleable#View_focusableInTouchMode |
| 594 | * @attr ref android.R.styleable#View_hapticFeedbackEnabled |
| 595 | * @attr ref android.R.styleable#View_keepScreenOn |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 596 | * @attr ref android.R.styleable#View_layerType |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 597 | * @attr ref android.R.styleable#View_longClickable |
| 598 | * @attr ref android.R.styleable#View_minHeight |
| 599 | * @attr ref android.R.styleable#View_minWidth |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | * @attr ref android.R.styleable#View_nextFocusDown |
| 601 | * @attr ref android.R.styleable#View_nextFocusLeft |
| 602 | * @attr ref android.R.styleable#View_nextFocusRight |
| 603 | * @attr ref android.R.styleable#View_nextFocusUp |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 604 | * @attr ref android.R.styleable#View_onClick |
| 605 | * @attr ref android.R.styleable#View_padding |
| 606 | * @attr ref android.R.styleable#View_paddingBottom |
| 607 | * @attr ref android.R.styleable#View_paddingLeft |
| 608 | * @attr ref android.R.styleable#View_paddingRight |
| 609 | * @attr ref android.R.styleable#View_paddingTop |
| 610 | * @attr ref android.R.styleable#View_saveEnabled |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 611 | * @attr ref android.R.styleable#View_rotation |
| 612 | * @attr ref android.R.styleable#View_rotationX |
| 613 | * @attr ref android.R.styleable#View_rotationY |
| 614 | * @attr ref android.R.styleable#View_scaleX |
| 615 | * @attr ref android.R.styleable#View_scaleY |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | * @attr ref android.R.styleable#View_scrollX |
| 617 | * @attr ref android.R.styleable#View_scrollY |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 618 | * @attr ref android.R.styleable#View_scrollbarSize |
| 619 | * @attr ref android.R.styleable#View_scrollbarStyle |
| 620 | * @attr ref android.R.styleable#View_scrollbars |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 621 | * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade |
| 622 | * @attr ref android.R.styleable#View_scrollbarFadeDuration |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | * @attr ref android.R.styleable#View_scrollbarTrackHorizontal |
| 624 | * @attr ref android.R.styleable#View_scrollbarThumbHorizontal |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | * @attr ref android.R.styleable#View_scrollbarThumbVertical |
| 626 | * @attr ref android.R.styleable#View_scrollbarTrackVertical |
| 627 | * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack |
| 628 | * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 629 | * @attr ref android.R.styleable#View_soundEffectsEnabled |
| 630 | * @attr ref android.R.styleable#View_tag |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 631 | * @attr ref android.R.styleable#View_transformPivotX |
| 632 | * @attr ref android.R.styleable#View_transformPivotY |
| 633 | * @attr ref android.R.styleable#View_translationX |
| 634 | * @attr ref android.R.styleable#View_translationY |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 635 | * @attr ref android.R.styleable#View_visibility |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | * |
| 637 | * @see android.view.ViewGroup |
| 638 | */ |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 639 | public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | private static final boolean DBG = false; |
| 641 | |
| 642 | /** |
| 643 | * The logging tag used by this class with android.util.Log. |
| 644 | */ |
| 645 | protected static final String VIEW_LOG_TAG = "View"; |
| 646 | |
| 647 | /** |
| 648 | * Used to mark a View that has no ID. |
| 649 | */ |
| 650 | public static final int NO_ID = -1; |
| 651 | |
| 652 | /** |
| 653 | * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when |
| 654 | * calling setFlags. |
| 655 | */ |
| 656 | private static final int NOT_FOCUSABLE = 0x00000000; |
| 657 | |
| 658 | /** |
| 659 | * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling |
| 660 | * setFlags. |
| 661 | */ |
| 662 | private static final int FOCUSABLE = 0x00000001; |
| 663 | |
| 664 | /** |
| 665 | * Mask for use with setFlags indicating bits used for focus. |
| 666 | */ |
| 667 | private static final int FOCUSABLE_MASK = 0x00000001; |
| 668 | |
| 669 | /** |
| 670 | * This view will adjust its padding to fit sytem windows (e.g. status bar) |
| 671 | */ |
| 672 | private static final int FITS_SYSTEM_WINDOWS = 0x00000002; |
| 673 | |
| 674 | /** |
Scott Main | 812634c2 | 2011-07-27 13:22:35 -0700 | [diff] [blame] | 675 | * This view is visible. |
| 676 | * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code |
| 677 | * android:visibility}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | */ |
| 679 | public static final int VISIBLE = 0x00000000; |
| 680 | |
| 681 | /** |
| 682 | * This view is invisible, but it still takes up space for layout purposes. |
Scott Main | 812634c2 | 2011-07-27 13:22:35 -0700 | [diff] [blame] | 683 | * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code |
| 684 | * android:visibility}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | */ |
| 686 | public static final int INVISIBLE = 0x00000004; |
| 687 | |
| 688 | /** |
| 689 | * This view is invisible, and it doesn't take any space for layout |
Scott Main | 812634c2 | 2011-07-27 13:22:35 -0700 | [diff] [blame] | 690 | * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code |
| 691 | * android:visibility}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | */ |
| 693 | public static final int GONE = 0x00000008; |
| 694 | |
| 695 | /** |
| 696 | * Mask for use with setFlags indicating bits used for visibility. |
| 697 | * {@hide} |
| 698 | */ |
| 699 | static final int VISIBILITY_MASK = 0x0000000C; |
| 700 | |
| 701 | private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE}; |
| 702 | |
| 703 | /** |
| 704 | * This view is enabled. Intrepretation varies by subclass. |
| 705 | * Use with ENABLED_MASK when calling setFlags. |
| 706 | * {@hide} |
| 707 | */ |
| 708 | static final int ENABLED = 0x00000000; |
| 709 | |
| 710 | /** |
| 711 | * This view is disabled. Intrepretation varies by subclass. |
| 712 | * Use with ENABLED_MASK when calling setFlags. |
| 713 | * {@hide} |
| 714 | */ |
| 715 | static final int DISABLED = 0x00000020; |
| 716 | |
| 717 | /** |
| 718 | * Mask for use with setFlags indicating bits used for indicating whether |
| 719 | * this view is enabled |
| 720 | * {@hide} |
| 721 | */ |
| 722 | static final int ENABLED_MASK = 0x00000020; |
| 723 | |
| 724 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 725 | * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be |
| 726 | * called and further optimizations will be performed. It is okay to have |
| 727 | * this flag set and a background. Use with DRAW_MASK when calling setFlags. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | * {@hide} |
| 729 | */ |
| 730 | static final int WILL_NOT_DRAW = 0x00000080; |
| 731 | |
| 732 | /** |
| 733 | * Mask for use with setFlags indicating bits used for indicating whether |
| 734 | * this view is will draw |
| 735 | * {@hide} |
| 736 | */ |
| 737 | static final int DRAW_MASK = 0x00000080; |
| 738 | |
| 739 | /** |
| 740 | * <p>This view doesn't show scrollbars.</p> |
| 741 | * {@hide} |
| 742 | */ |
| 743 | static final int SCROLLBARS_NONE = 0x00000000; |
| 744 | |
| 745 | /** |
| 746 | * <p>This view shows horizontal scrollbars.</p> |
| 747 | * {@hide} |
| 748 | */ |
| 749 | static final int SCROLLBARS_HORIZONTAL = 0x00000100; |
| 750 | |
| 751 | /** |
| 752 | * <p>This view shows vertical scrollbars.</p> |
| 753 | * {@hide} |
| 754 | */ |
| 755 | static final int SCROLLBARS_VERTICAL = 0x00000200; |
| 756 | |
| 757 | /** |
| 758 | * <p>Mask for use with setFlags indicating bits used for indicating which |
| 759 | * scrollbars are enabled.</p> |
| 760 | * {@hide} |
| 761 | */ |
| 762 | static final int SCROLLBARS_MASK = 0x00000300; |
| 763 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 764 | /** |
| 765 | * Indicates that the view should filter touches when its window is obscured. |
| 766 | * Refer to the class comments for more information about this security feature. |
| 767 | * {@hide} |
| 768 | */ |
| 769 | static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400; |
| 770 | |
| 771 | // note flag value 0x00000800 is now available for next flags... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | |
| 773 | /** |
| 774 | * <p>This view doesn't show fading edges.</p> |
| 775 | * {@hide} |
| 776 | */ |
| 777 | static final int FADING_EDGE_NONE = 0x00000000; |
| 778 | |
| 779 | /** |
| 780 | * <p>This view shows horizontal fading edges.</p> |
| 781 | * {@hide} |
| 782 | */ |
| 783 | static final int FADING_EDGE_HORIZONTAL = 0x00001000; |
| 784 | |
| 785 | /** |
| 786 | * <p>This view shows vertical fading edges.</p> |
| 787 | * {@hide} |
| 788 | */ |
| 789 | static final int FADING_EDGE_VERTICAL = 0x00002000; |
| 790 | |
| 791 | /** |
| 792 | * <p>Mask for use with setFlags indicating bits used for indicating which |
| 793 | * fading edges are enabled.</p> |
| 794 | * {@hide} |
| 795 | */ |
| 796 | static final int FADING_EDGE_MASK = 0x00003000; |
| 797 | |
| 798 | /** |
| 799 | * <p>Indicates this view can be clicked. When clickable, a View reacts |
| 800 | * to clicks by notifying the OnClickListener.<p> |
| 801 | * {@hide} |
| 802 | */ |
| 803 | static final int CLICKABLE = 0x00004000; |
| 804 | |
| 805 | /** |
| 806 | * <p>Indicates this view is caching its drawing into a bitmap.</p> |
| 807 | * {@hide} |
| 808 | */ |
| 809 | static final int DRAWING_CACHE_ENABLED = 0x00008000; |
| 810 | |
| 811 | /** |
| 812 | * <p>Indicates that no icicle should be saved for this view.<p> |
| 813 | * {@hide} |
| 814 | */ |
| 815 | static final int SAVE_DISABLED = 0x000010000; |
| 816 | |
| 817 | /** |
| 818 | * <p>Mask for use with setFlags indicating bits used for the saveEnabled |
| 819 | * property.</p> |
| 820 | * {@hide} |
| 821 | */ |
| 822 | static final int SAVE_DISABLED_MASK = 0x000010000; |
| 823 | |
| 824 | /** |
| 825 | * <p>Indicates that no drawing cache should ever be created for this view.<p> |
| 826 | * {@hide} |
| 827 | */ |
| 828 | static final int WILL_NOT_CACHE_DRAWING = 0x000020000; |
| 829 | |
| 830 | /** |
| 831 | * <p>Indicates this view can take / keep focus when int touch mode.</p> |
| 832 | * {@hide} |
| 833 | */ |
| 834 | static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000; |
| 835 | |
| 836 | /** |
| 837 | * <p>Enables low quality mode for the drawing cache.</p> |
| 838 | */ |
| 839 | public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000; |
| 840 | |
| 841 | /** |
| 842 | * <p>Enables high quality mode for the drawing cache.</p> |
| 843 | */ |
| 844 | public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000; |
| 845 | |
| 846 | /** |
| 847 | * <p>Enables automatic quality mode for the drawing cache.</p> |
| 848 | */ |
| 849 | public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000; |
| 850 | |
| 851 | private static final int[] DRAWING_CACHE_QUALITY_FLAGS = { |
| 852 | DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH |
| 853 | }; |
| 854 | |
| 855 | /** |
| 856 | * <p>Mask for use with setFlags indicating bits used for the cache |
| 857 | * quality property.</p> |
| 858 | * {@hide} |
| 859 | */ |
| 860 | static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000; |
| 861 | |
| 862 | /** |
| 863 | * <p> |
| 864 | * Indicates this view can be long clicked. When long clickable, a View |
| 865 | * reacts to long clicks by notifying the OnLongClickListener or showing a |
| 866 | * context menu. |
| 867 | * </p> |
| 868 | * {@hide} |
| 869 | */ |
| 870 | static final int LONG_CLICKABLE = 0x00200000; |
| 871 | |
| 872 | /** |
| 873 | * <p>Indicates that this view gets its drawable states from its direct parent |
| 874 | * and ignores its original internal states.</p> |
| 875 | * |
| 876 | * @hide |
| 877 | */ |
| 878 | static final int DUPLICATE_PARENT_STATE = 0x00400000; |
| 879 | |
| 880 | /** |
| 881 | * The scrollbar style to display the scrollbars inside the content area, |
| 882 | * without increasing the padding. The scrollbars will be overlaid with |
| 883 | * translucency on the view's content. |
| 884 | */ |
| 885 | public static final int SCROLLBARS_INSIDE_OVERLAY = 0; |
| 886 | |
| 887 | /** |
| 888 | * The scrollbar style to display the scrollbars inside the padded area, |
| 889 | * increasing the padding of the view. The scrollbars will not overlap the |
| 890 | * content area of the view. |
| 891 | */ |
| 892 | public static final int SCROLLBARS_INSIDE_INSET = 0x01000000; |
| 893 | |
| 894 | /** |
| 895 | * The scrollbar style to display the scrollbars at the edge of the view, |
| 896 | * without increasing the padding. The scrollbars will be overlaid with |
| 897 | * translucency. |
| 898 | */ |
| 899 | public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000; |
| 900 | |
| 901 | /** |
| 902 | * The scrollbar style to display the scrollbars at the edge of the view, |
| 903 | * increasing the padding of the view. The scrollbars will only overlap the |
| 904 | * background, if any. |
| 905 | */ |
| 906 | public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000; |
| 907 | |
| 908 | /** |
| 909 | * Mask to check if the scrollbar style is overlay or inset. |
| 910 | * {@hide} |
| 911 | */ |
| 912 | static final int SCROLLBARS_INSET_MASK = 0x01000000; |
| 913 | |
| 914 | /** |
| 915 | * Mask to check if the scrollbar style is inside or outside. |
| 916 | * {@hide} |
| 917 | */ |
| 918 | static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000; |
| 919 | |
| 920 | /** |
| 921 | * Mask for scrollbar style. |
| 922 | * {@hide} |
| 923 | */ |
| 924 | static final int SCROLLBARS_STYLE_MASK = 0x03000000; |
| 925 | |
| 926 | /** |
| 927 | * View flag indicating that the screen should remain on while the |
| 928 | * window containing this view is visible to the user. This effectively |
| 929 | * takes care of automatically setting the WindowManager's |
| 930 | * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}. |
| 931 | */ |
| 932 | public static final int KEEP_SCREEN_ON = 0x04000000; |
| 933 | |
| 934 | /** |
| 935 | * View flag indicating whether this view should have sound effects enabled |
| 936 | * for events such as clicking and touching. |
| 937 | */ |
| 938 | public static final int SOUND_EFFECTS_ENABLED = 0x08000000; |
| 939 | |
| 940 | /** |
| 941 | * View flag indicating whether this view should have haptic feedback |
| 942 | * enabled for events such as long presses. |
| 943 | */ |
| 944 | public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000; |
| 945 | |
| 946 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 947 | * <p>Indicates that the view hierarchy should stop saving state when |
| 948 | * it reaches this view. If state saving is initiated immediately at |
| 949 | * the view, it will be allowed. |
| 950 | * {@hide} |
| 951 | */ |
| 952 | static final int PARENT_SAVE_DISABLED = 0x20000000; |
| 953 | |
| 954 | /** |
| 955 | * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p> |
| 956 | * {@hide} |
| 957 | */ |
| 958 | static final int PARENT_SAVE_DISABLED_MASK = 0x20000000; |
| 959 | |
| 960 | /** |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 961 | * Horizontal direction of this view is from Left to Right. |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 962 | * Use with {@link #setLayoutDirection}. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 963 | * {@hide} |
| 964 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 965 | public static final int LAYOUT_DIRECTION_LTR = 0x00000000; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 966 | |
| 967 | /** |
| 968 | * Horizontal direction of this view is from Right to Left. |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 969 | * Use with {@link #setLayoutDirection}. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 970 | * {@hide} |
| 971 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 972 | public static final int LAYOUT_DIRECTION_RTL = 0x40000000; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 973 | |
| 974 | /** |
| 975 | * Horizontal direction of this view is inherited from its parent. |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 976 | * Use with {@link #setLayoutDirection}. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 977 | * {@hide} |
| 978 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 979 | public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 980 | |
| 981 | /** |
| 982 | * Horizontal direction of this view is from deduced from the default language |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 983 | * script for the locale. Use with {@link #setLayoutDirection}. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 984 | * {@hide} |
| 985 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 986 | public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 987 | |
| 988 | /** |
| 989 | * Mask for use with setFlags indicating bits used for horizontalDirection. |
| 990 | * {@hide} |
| 991 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 992 | static final int LAYOUT_DIRECTION_MASK = 0xC0000000; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 993 | |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 994 | /* |
| 995 | * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct |
| 996 | * flag value. |
| 997 | * {@hide} |
| 998 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 999 | private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR, |
| 1000 | LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE}; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 1001 | |
| 1002 | /** |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 1003 | * Default horizontalDirection. |
| 1004 | * {@hide} |
| 1005 | */ |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 1006 | private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT; |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 1007 | |
| 1008 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1009 | * View flag indicating whether {@link #addFocusables(ArrayList, int, int)} |
| 1010 | * should add all focusable Views regardless if they are focusable in touch mode. |
| 1011 | */ |
| 1012 | public static final int FOCUSABLES_ALL = 0x00000000; |
| 1013 | |
| 1014 | /** |
| 1015 | * View flag indicating whether {@link #addFocusables(ArrayList, int, int)} |
| 1016 | * should add only Views focusable in touch mode. |
| 1017 | */ |
| 1018 | public static final int FOCUSABLES_TOUCH_MODE = 0x00000001; |
| 1019 | |
| 1020 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1021 | * Use with {@link #focusSearch(int)}. Move focus to the previous selectable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | * item. |
| 1023 | */ |
| 1024 | public static final int FOCUS_BACKWARD = 0x00000001; |
| 1025 | |
| 1026 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1027 | * Use with {@link #focusSearch(int)}. Move focus to the next selectable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | * item. |
| 1029 | */ |
| 1030 | public static final int FOCUS_FORWARD = 0x00000002; |
| 1031 | |
| 1032 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1033 | * Use with {@link #focusSearch(int)}. Move focus to the left. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | */ |
| 1035 | public static final int FOCUS_LEFT = 0x00000011; |
| 1036 | |
| 1037 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1038 | * Use with {@link #focusSearch(int)}. Move focus up. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | */ |
| 1040 | public static final int FOCUS_UP = 0x00000021; |
| 1041 | |
| 1042 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1043 | * Use with {@link #focusSearch(int)}. Move focus to the right. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | */ |
| 1045 | public static final int FOCUS_RIGHT = 0x00000042; |
| 1046 | |
| 1047 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 1048 | * Use with {@link #focusSearch(int)}. Move focus down. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | */ |
| 1050 | public static final int FOCUS_DOWN = 0x00000082; |
| 1051 | |
| 1052 | /** |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1053 | * Bits of {@link #getMeasuredWidthAndState()} and |
| 1054 | * {@link #getMeasuredWidthAndState()} that provide the actual measured size. |
| 1055 | */ |
| 1056 | public static final int MEASURED_SIZE_MASK = 0x00ffffff; |
| 1057 | |
| 1058 | /** |
| 1059 | * Bits of {@link #getMeasuredWidthAndState()} and |
| 1060 | * {@link #getMeasuredWidthAndState()} that provide the additional state bits. |
| 1061 | */ |
| 1062 | public static final int MEASURED_STATE_MASK = 0xff000000; |
| 1063 | |
| 1064 | /** |
| 1065 | * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits |
| 1066 | * for functions that combine both width and height into a single int, |
| 1067 | * such as {@link #getMeasuredState()} and the childState argument of |
| 1068 | * {@link #resolveSizeAndState(int, int, int)}. |
| 1069 | */ |
| 1070 | public static final int MEASURED_HEIGHT_STATE_SHIFT = 16; |
| 1071 | |
| 1072 | /** |
| 1073 | * Bit of {@link #getMeasuredWidthAndState()} and |
| 1074 | * {@link #getMeasuredWidthAndState()} that indicates the measured size |
| 1075 | * is smaller that the space the view would like to have. |
| 1076 | */ |
| 1077 | public static final int MEASURED_STATE_TOO_SMALL = 0x01000000; |
| 1078 | |
| 1079 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | * Base View state sets |
| 1081 | */ |
| 1082 | // Singles |
| 1083 | /** |
| 1084 | * Indicates the view has no states set. States are used with |
| 1085 | * {@link android.graphics.drawable.Drawable} to change the drawing of the |
| 1086 | * view depending on its state. |
| 1087 | * |
| 1088 | * @see android.graphics.drawable.Drawable |
| 1089 | * @see #getDrawableState() |
| 1090 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1091 | protected static final int[] EMPTY_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | /** |
| 1093 | * Indicates the view is enabled. States are used with |
| 1094 | * {@link android.graphics.drawable.Drawable} to change the drawing of the |
| 1095 | * view depending on its state. |
| 1096 | * |
| 1097 | * @see android.graphics.drawable.Drawable |
| 1098 | * @see #getDrawableState() |
| 1099 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1100 | protected static final int[] ENABLED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | /** |
| 1102 | * Indicates the view is focused. States are used with |
| 1103 | * {@link android.graphics.drawable.Drawable} to change the drawing of the |
| 1104 | * view depending on its state. |
| 1105 | * |
| 1106 | * @see android.graphics.drawable.Drawable |
| 1107 | * @see #getDrawableState() |
| 1108 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1109 | protected static final int[] FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | /** |
| 1111 | * Indicates the view is selected. States are used with |
| 1112 | * {@link android.graphics.drawable.Drawable} to change the drawing of the |
| 1113 | * view depending on its state. |
| 1114 | * |
| 1115 | * @see android.graphics.drawable.Drawable |
| 1116 | * @see #getDrawableState() |
| 1117 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1118 | protected static final int[] SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | /** |
| 1120 | * Indicates the view is pressed. States are used with |
| 1121 | * {@link android.graphics.drawable.Drawable} to change the drawing of the |
| 1122 | * view depending on its state. |
| 1123 | * |
| 1124 | * @see android.graphics.drawable.Drawable |
| 1125 | * @see #getDrawableState() |
| 1126 | * @hide |
| 1127 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1128 | protected static final int[] PRESSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | /** |
| 1130 | * Indicates the view's window has focus. 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 Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1137 | protected static final int[] WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | // Doubles |
| 1139 | /** |
| 1140 | * Indicates the view is enabled and has the focus. |
| 1141 | * |
| 1142 | * @see #ENABLED_STATE_SET |
| 1143 | * @see #FOCUSED_STATE_SET |
| 1144 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1145 | protected static final int[] ENABLED_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | /** |
| 1147 | * Indicates the view is enabled and selected. |
| 1148 | * |
| 1149 | * @see #ENABLED_STATE_SET |
| 1150 | * @see #SELECTED_STATE_SET |
| 1151 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1152 | protected static final int[] ENABLED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | /** |
| 1154 | * Indicates the view is enabled and that its window has focus. |
| 1155 | * |
| 1156 | * @see #ENABLED_STATE_SET |
| 1157 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1158 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1159 | protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | /** |
| 1161 | * Indicates the view is focused and selected. |
| 1162 | * |
| 1163 | * @see #FOCUSED_STATE_SET |
| 1164 | * @see #SELECTED_STATE_SET |
| 1165 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1166 | protected static final int[] FOCUSED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | /** |
| 1168 | * Indicates the view has the focus and that its window has the focus. |
| 1169 | * |
| 1170 | * @see #FOCUSED_STATE_SET |
| 1171 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1172 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1173 | protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | /** |
| 1175 | * Indicates the view is selected and that its window has the focus. |
| 1176 | * |
| 1177 | * @see #SELECTED_STATE_SET |
| 1178 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1179 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1180 | protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | // Triples |
| 1182 | /** |
| 1183 | * Indicates the view is enabled, focused and selected. |
| 1184 | * |
| 1185 | * @see #ENABLED_STATE_SET |
| 1186 | * @see #FOCUSED_STATE_SET |
| 1187 | * @see #SELECTED_STATE_SET |
| 1188 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1189 | protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | /** |
| 1191 | * Indicates the view is enabled, focused and its window has the focus. |
| 1192 | * |
| 1193 | * @see #ENABLED_STATE_SET |
| 1194 | * @see #FOCUSED_STATE_SET |
| 1195 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1196 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1197 | protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | /** |
| 1199 | * Indicates the view is enabled, selected and its window has the focus. |
| 1200 | * |
| 1201 | * @see #ENABLED_STATE_SET |
| 1202 | * @see #SELECTED_STATE_SET |
| 1203 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1204 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1205 | protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | /** |
| 1207 | * Indicates the view is focused, selected and its window has the focus. |
| 1208 | * |
| 1209 | * @see #FOCUSED_STATE_SET |
| 1210 | * @see #SELECTED_STATE_SET |
| 1211 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1212 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1213 | protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | /** |
| 1215 | * Indicates the view is enabled, focused, selected and its window |
| 1216 | * has the focus. |
| 1217 | * |
| 1218 | * @see #ENABLED_STATE_SET |
| 1219 | * @see #FOCUSED_STATE_SET |
| 1220 | * @see #SELECTED_STATE_SET |
| 1221 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1222 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1223 | protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | /** |
| 1225 | * Indicates the view is pressed and its window has the focus. |
| 1226 | * |
| 1227 | * @see #PRESSED_STATE_SET |
| 1228 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1229 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1230 | protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 | /** |
| 1232 | * Indicates the view is pressed and selected. |
| 1233 | * |
| 1234 | * @see #PRESSED_STATE_SET |
| 1235 | * @see #SELECTED_STATE_SET |
| 1236 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1237 | protected static final int[] PRESSED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | /** |
| 1239 | * Indicates the view is pressed, selected and its window has the focus. |
| 1240 | * |
| 1241 | * @see #PRESSED_STATE_SET |
| 1242 | * @see #SELECTED_STATE_SET |
| 1243 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1244 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1245 | protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | /** |
| 1247 | * Indicates the view is pressed and focused. |
| 1248 | * |
| 1249 | * @see #PRESSED_STATE_SET |
| 1250 | * @see #FOCUSED_STATE_SET |
| 1251 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1252 | protected static final int[] PRESSED_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | /** |
| 1254 | * Indicates the view is pressed, focused and its window has the focus. |
| 1255 | * |
| 1256 | * @see #PRESSED_STATE_SET |
| 1257 | * @see #FOCUSED_STATE_SET |
| 1258 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1259 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1260 | protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1261 | /** |
| 1262 | * Indicates the view is pressed, focused and selected. |
| 1263 | * |
| 1264 | * @see #PRESSED_STATE_SET |
| 1265 | * @see #SELECTED_STATE_SET |
| 1266 | * @see #FOCUSED_STATE_SET |
| 1267 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1268 | protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | /** |
| 1270 | * Indicates the view is pressed, focused, selected and its window has the focus. |
| 1271 | * |
| 1272 | * @see #PRESSED_STATE_SET |
| 1273 | * @see #FOCUSED_STATE_SET |
| 1274 | * @see #SELECTED_STATE_SET |
| 1275 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1276 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1277 | protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | /** |
| 1279 | * Indicates the view is pressed and enabled. |
| 1280 | * |
| 1281 | * @see #PRESSED_STATE_SET |
| 1282 | * @see #ENABLED_STATE_SET |
| 1283 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1284 | protected static final int[] PRESSED_ENABLED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | /** |
| 1286 | * Indicates the view is pressed, enabled and its window has the focus. |
| 1287 | * |
| 1288 | * @see #PRESSED_STATE_SET |
| 1289 | * @see #ENABLED_STATE_SET |
| 1290 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1291 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1292 | protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | /** |
| 1294 | * Indicates the view is pressed, enabled and selected. |
| 1295 | * |
| 1296 | * @see #PRESSED_STATE_SET |
| 1297 | * @see #ENABLED_STATE_SET |
| 1298 | * @see #SELECTED_STATE_SET |
| 1299 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1300 | protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | /** |
| 1302 | * Indicates the view is pressed, enabled, selected and its window has the |
| 1303 | * focus. |
| 1304 | * |
| 1305 | * @see #PRESSED_STATE_SET |
| 1306 | * @see #ENABLED_STATE_SET |
| 1307 | * @see #SELECTED_STATE_SET |
| 1308 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1309 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1310 | protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | /** |
| 1312 | * Indicates the view is pressed, enabled and focused. |
| 1313 | * |
| 1314 | * @see #PRESSED_STATE_SET |
| 1315 | * @see #ENABLED_STATE_SET |
| 1316 | * @see #FOCUSED_STATE_SET |
| 1317 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1318 | protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | /** |
| 1320 | * Indicates the view is pressed, enabled, focused and its window has the |
| 1321 | * focus. |
| 1322 | * |
| 1323 | * @see #PRESSED_STATE_SET |
| 1324 | * @see #ENABLED_STATE_SET |
| 1325 | * @see #FOCUSED_STATE_SET |
| 1326 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1327 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1328 | protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | /** |
| 1330 | * Indicates the view is pressed, enabled, focused and selected. |
| 1331 | * |
| 1332 | * @see #PRESSED_STATE_SET |
| 1333 | * @see #ENABLED_STATE_SET |
| 1334 | * @see #SELECTED_STATE_SET |
| 1335 | * @see #FOCUSED_STATE_SET |
| 1336 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1337 | protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | /** |
| 1339 | * Indicates the view is pressed, enabled, focused, selected and its window |
| 1340 | * has the focus. |
| 1341 | * |
| 1342 | * @see #PRESSED_STATE_SET |
| 1343 | * @see #ENABLED_STATE_SET |
| 1344 | * @see #SELECTED_STATE_SET |
| 1345 | * @see #FOCUSED_STATE_SET |
| 1346 | * @see #WINDOW_FOCUSED_STATE_SET |
| 1347 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1348 | protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | |
| 1350 | /** |
| 1351 | * The order here is very important to {@link #getDrawableState()} |
| 1352 | */ |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1353 | private static final int[][] VIEW_STATE_SETS; |
| 1354 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1355 | static final int VIEW_STATE_WINDOW_FOCUSED = 1; |
| 1356 | static final int VIEW_STATE_SELECTED = 1 << 1; |
| 1357 | static final int VIEW_STATE_FOCUSED = 1 << 2; |
| 1358 | static final int VIEW_STATE_ENABLED = 1 << 3; |
| 1359 | static final int VIEW_STATE_PRESSED = 1 << 4; |
| 1360 | static final int VIEW_STATE_ACTIVATED = 1 << 5; |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 1361 | static final int VIEW_STATE_ACCELERATED = 1 << 6; |
PY Laligand | c33d8d49e | 2011-03-14 18:22:53 -0700 | [diff] [blame] | 1362 | static final int VIEW_STATE_HOVERED = 1 << 7; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1363 | static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8; |
| 1364 | static final int VIEW_STATE_DRAG_HOVERED = 1 << 9; |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1365 | |
| 1366 | static final int[] VIEW_STATE_IDS = new int[] { |
| 1367 | R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED, |
| 1368 | R.attr.state_selected, VIEW_STATE_SELECTED, |
| 1369 | R.attr.state_focused, VIEW_STATE_FOCUSED, |
| 1370 | R.attr.state_enabled, VIEW_STATE_ENABLED, |
| 1371 | R.attr.state_pressed, VIEW_STATE_PRESSED, |
| 1372 | R.attr.state_activated, VIEW_STATE_ACTIVATED, |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 1373 | R.attr.state_accelerated, VIEW_STATE_ACCELERATED, |
PY Laligand | c33d8d49e | 2011-03-14 18:22:53 -0700 | [diff] [blame] | 1374 | R.attr.state_hovered, VIEW_STATE_HOVERED, |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1375 | R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT, |
| 1376 | R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | }; |
| 1378 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1379 | static { |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 1380 | if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) { |
| 1381 | throw new IllegalStateException( |
| 1382 | "VIEW_STATE_IDs array length does not match ViewDrawableStates style array"); |
| 1383 | } |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1384 | int[] orderedIds = new int[VIEW_STATE_IDS.length]; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1385 | for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1386 | int viewState = R.styleable.ViewDrawableStates[i]; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1387 | for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1388 | if (VIEW_STATE_IDS[j] == viewState) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1389 | orderedIds[i * 2] = viewState; |
| 1390 | orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1]; |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | } |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1394 | final int NUM_BITS = VIEW_STATE_IDS.length / 2; |
| 1395 | VIEW_STATE_SETS = new int[1 << NUM_BITS][]; |
| 1396 | for (int i = 0; i < VIEW_STATE_SETS.length; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1397 | int numBits = Integer.bitCount(i); |
| 1398 | int[] set = new int[numBits]; |
| 1399 | int pos = 0; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1400 | for (int j = 0; j < orderedIds.length; j += 2) { |
| 1401 | if ((i & orderedIds[j+1]) != 0) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1402 | set[pos++] = orderedIds[j]; |
| 1403 | } |
| 1404 | } |
| 1405 | VIEW_STATE_SETS[i] = set; |
| 1406 | } |
| 1407 | |
| 1408 | EMPTY_STATE_SET = VIEW_STATE_SETS[0]; |
| 1409 | WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED]; |
| 1410 | SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED]; |
| 1411 | SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1412 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED]; |
| 1413 | FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED]; |
| 1414 | FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1415 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED]; |
| 1416 | FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1417 | VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED]; |
| 1418 | FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1419 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1420 | | VIEW_STATE_FOCUSED]; |
| 1421 | ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED]; |
| 1422 | ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1423 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED]; |
| 1424 | ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1425 | VIEW_STATE_SELECTED | VIEW_STATE_ENABLED]; |
| 1426 | ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1427 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1428 | | VIEW_STATE_ENABLED]; |
| 1429 | ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1430 | VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED]; |
| 1431 | ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1432 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED |
| 1433 | | VIEW_STATE_ENABLED]; |
| 1434 | ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1435 | VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED |
| 1436 | | VIEW_STATE_ENABLED]; |
| 1437 | ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1438 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1439 | | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED]; |
| 1440 | |
| 1441 | PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED]; |
| 1442 | PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1443 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED]; |
| 1444 | PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1445 | VIEW_STATE_SELECTED | VIEW_STATE_PRESSED]; |
| 1446 | PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1447 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1448 | | VIEW_STATE_PRESSED]; |
| 1449 | PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1450 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED]; |
| 1451 | PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1452 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED |
| 1453 | | VIEW_STATE_PRESSED]; |
| 1454 | PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1455 | VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED |
| 1456 | | VIEW_STATE_PRESSED]; |
| 1457 | PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1458 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1459 | | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED]; |
| 1460 | PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[ |
| 1461 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED]; |
| 1462 | PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1463 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED |
| 1464 | | VIEW_STATE_PRESSED]; |
| 1465 | PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1466 | VIEW_STATE_SELECTED | VIEW_STATE_ENABLED |
| 1467 | | VIEW_STATE_PRESSED]; |
| 1468 | PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1469 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1470 | | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED]; |
| 1471 | PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1472 | VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED |
| 1473 | | VIEW_STATE_PRESSED]; |
| 1474 | PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1475 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED |
| 1476 | | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED]; |
| 1477 | PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[ |
| 1478 | VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED |
| 1479 | | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED]; |
| 1480 | PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[ |
| 1481 | VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED |
| 1482 | | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED |
| 1483 | | VIEW_STATE_PRESSED]; |
| 1484 | } |
| 1485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | * Temporary Rect currently for use in setBackground(). This will probably |
| 1488 | * be extended in the future to hold our own class with more than just |
| 1489 | * a Rect. :) |
| 1490 | */ |
| 1491 | static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>(); |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 1492 | |
| 1493 | /** |
| 1494 | * Map used to store views' tags. |
| 1495 | */ |
| 1496 | private static WeakHashMap<View, SparseArray<Object>> sTags; |
| 1497 | |
| 1498 | /** |
| 1499 | * Lock used to access sTags. |
| 1500 | */ |
| 1501 | private static final Object sTagsLock = new Object(); |
| 1502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1504 | * The next available accessiiblity id. |
| 1505 | */ |
| 1506 | private static int sNextAccessibilityViewId; |
| 1507 | |
| 1508 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | * The animation currently associated with this view. |
| 1510 | * @hide |
| 1511 | */ |
| 1512 | protected Animation mCurrentAnimation = null; |
| 1513 | |
| 1514 | /** |
| 1515 | * Width as measured during measure pass. |
| 1516 | * {@hide} |
| 1517 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1518 | @ViewDebug.ExportedProperty(category = "measurement") |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 1519 | int mMeasuredWidth; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | |
| 1521 | /** |
| 1522 | * Height as measured during measure pass. |
| 1523 | * {@hide} |
| 1524 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1525 | @ViewDebug.ExportedProperty(category = "measurement") |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 1526 | int mMeasuredHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | |
| 1528 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1529 | * Flag to indicate that this view was marked INVALIDATED, or had its display list |
| 1530 | * invalidated, prior to the current drawing iteration. If true, the view must re-draw |
| 1531 | * its display list. This flag, used only when hw accelerated, allows us to clear the |
| 1532 | * flag while retaining this information until it's needed (at getDisplayList() time and |
| 1533 | * in drawChild(), when we decide to draw a view's children's display lists into our own). |
| 1534 | * |
| 1535 | * {@hide} |
| 1536 | */ |
| 1537 | boolean mRecreateDisplayList = false; |
| 1538 | |
| 1539 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | * The view's identifier. |
| 1541 | * {@hide} |
| 1542 | * |
| 1543 | * @see #setId(int) |
| 1544 | * @see #getId() |
| 1545 | */ |
| 1546 | @ViewDebug.ExportedProperty(resolveId = true) |
| 1547 | int mID = NO_ID; |
| 1548 | |
| 1549 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1550 | * The stable ID of this view for accessibility porposes. |
| 1551 | */ |
| 1552 | int mAccessibilityViewId = NO_ID; |
| 1553 | |
| 1554 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | * The view's tag. |
| 1556 | * {@hide} |
| 1557 | * |
| 1558 | * @see #setTag(Object) |
| 1559 | * @see #getTag() |
| 1560 | */ |
| 1561 | protected Object mTag; |
| 1562 | |
| 1563 | // for mPrivateFlags: |
| 1564 | /** {@hide} */ |
| 1565 | static final int WANTS_FOCUS = 0x00000001; |
| 1566 | /** {@hide} */ |
| 1567 | static final int FOCUSED = 0x00000002; |
| 1568 | /** {@hide} */ |
| 1569 | static final int SELECTED = 0x00000004; |
| 1570 | /** {@hide} */ |
| 1571 | static final int IS_ROOT_NAMESPACE = 0x00000008; |
| 1572 | /** {@hide} */ |
| 1573 | static final int HAS_BOUNDS = 0x00000010; |
| 1574 | /** {@hide} */ |
| 1575 | static final int DRAWN = 0x00000020; |
| 1576 | /** |
| 1577 | * When this flag is set, this view is running an animation on behalf of its |
| 1578 | * children and should therefore not cancel invalidate requests, even if they |
| 1579 | * lie outside of this view's bounds. |
| 1580 | * |
| 1581 | * {@hide} |
| 1582 | */ |
| 1583 | static final int DRAW_ANIMATION = 0x00000040; |
| 1584 | /** {@hide} */ |
| 1585 | static final int SKIP_DRAW = 0x00000080; |
| 1586 | /** {@hide} */ |
| 1587 | static final int ONLY_DRAWS_BACKGROUND = 0x00000100; |
| 1588 | /** {@hide} */ |
| 1589 | static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200; |
| 1590 | /** {@hide} */ |
| 1591 | static final int DRAWABLE_STATE_DIRTY = 0x00000400; |
| 1592 | /** {@hide} */ |
| 1593 | static final int MEASURED_DIMENSION_SET = 0x00000800; |
| 1594 | /** {@hide} */ |
| 1595 | static final int FORCE_LAYOUT = 0x00001000; |
Konstantin Lopyrev | c6dc457 | 2010-08-06 15:01:52 -0700 | [diff] [blame] | 1596 | /** {@hide} */ |
| 1597 | static final int LAYOUT_REQUIRED = 0x00002000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | |
| 1599 | private static final int PRESSED = 0x00004000; |
| 1600 | |
| 1601 | /** {@hide} */ |
| 1602 | static final int DRAWING_CACHE_VALID = 0x00008000; |
| 1603 | /** |
| 1604 | * Flag used to indicate that this view should be drawn once more (and only once |
| 1605 | * more) after its animation has completed. |
| 1606 | * {@hide} |
| 1607 | */ |
| 1608 | static final int ANIMATION_STARTED = 0x00010000; |
| 1609 | |
| 1610 | private static final int SAVE_STATE_CALLED = 0x00020000; |
| 1611 | |
| 1612 | /** |
| 1613 | * Indicates that the View returned true when onSetAlpha() was called and that |
| 1614 | * the alpha must be restored. |
| 1615 | * {@hide} |
| 1616 | */ |
| 1617 | static final int ALPHA_SET = 0x00040000; |
| 1618 | |
| 1619 | /** |
| 1620 | * Set by {@link #setScrollContainer(boolean)}. |
| 1621 | */ |
| 1622 | static final int SCROLL_CONTAINER = 0x00080000; |
| 1623 | |
| 1624 | /** |
| 1625 | * Set by {@link #setScrollContainer(boolean)}. |
| 1626 | */ |
| 1627 | static final int SCROLL_CONTAINER_ADDED = 0x00100000; |
| 1628 | |
| 1629 | /** |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1630 | * View flag indicating whether this view was invalidated (fully or partially.) |
| 1631 | * |
| 1632 | * @hide |
| 1633 | */ |
| 1634 | static final int DIRTY = 0x00200000; |
| 1635 | |
| 1636 | /** |
| 1637 | * View flag indicating whether this view was invalidated by an opaque |
| 1638 | * invalidate request. |
| 1639 | * |
| 1640 | * @hide |
| 1641 | */ |
| 1642 | static final int DIRTY_OPAQUE = 0x00400000; |
| 1643 | |
| 1644 | /** |
| 1645 | * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}. |
| 1646 | * |
| 1647 | * @hide |
| 1648 | */ |
| 1649 | static final int DIRTY_MASK = 0x00600000; |
| 1650 | |
| 1651 | /** |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 1652 | * Indicates whether the background is opaque. |
| 1653 | * |
| 1654 | * @hide |
| 1655 | */ |
| 1656 | static final int OPAQUE_BACKGROUND = 0x00800000; |
| 1657 | |
| 1658 | /** |
| 1659 | * Indicates whether the scrollbars are opaque. |
| 1660 | * |
| 1661 | * @hide |
| 1662 | */ |
| 1663 | static final int OPAQUE_SCROLLBARS = 0x01000000; |
| 1664 | |
| 1665 | /** |
| 1666 | * Indicates whether the view is opaque. |
| 1667 | * |
| 1668 | * @hide |
| 1669 | */ |
| 1670 | static final int OPAQUE_MASK = 0x01800000; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1671 | |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 1672 | /** |
| 1673 | * Indicates a prepressed state; |
| 1674 | * the short time between ACTION_DOWN and recognizing |
| 1675 | * a 'real' press. Prepressed is used to recognize quick taps |
| 1676 | * even when they are shorter than ViewConfiguration.getTapTimeout(). |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1677 | * |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 1678 | * @hide |
| 1679 | */ |
| 1680 | private static final int PREPRESSED = 0x02000000; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1681 | |
Adam Powell | c9fbaab | 2010-02-16 17:16:19 -0800 | [diff] [blame] | 1682 | /** |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 1683 | * Indicates whether the view is temporarily detached. |
| 1684 | * |
| 1685 | * @hide |
| 1686 | */ |
| 1687 | static final int CANCEL_NEXT_UP_EVENT = 0x04000000; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1688 | |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 1689 | /** |
| 1690 | * Indicates that we should awaken scroll bars once attached |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1691 | * |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 1692 | * @hide |
| 1693 | */ |
| 1694 | private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000; |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 1695 | |
| 1696 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1697 | * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT. |
| 1698 | * @hide |
| 1699 | */ |
| 1700 | private static final int HOVERED = 0x10000000; |
| 1701 | |
| 1702 | /** |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 1703 | * Indicates that pivotX or pivotY were explicitly set and we should not assume the center |
| 1704 | * for transform operations |
| 1705 | * |
| 1706 | * @hide |
| 1707 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 1708 | private static final int PIVOT_EXPLICITLY_SET = 0x20000000; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 1709 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1710 | /** {@hide} */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 1711 | static final int ACTIVATED = 0x40000000; |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 1712 | |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 1713 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1714 | * Indicates that this view was specifically invalidated, not just dirtied because some |
| 1715 | * child view was invalidated. The flag is used to determine when we need to recreate |
| 1716 | * a view's display list (as opposed to just returning a reference to its existing |
| 1717 | * display list). |
| 1718 | * |
| 1719 | * @hide |
| 1720 | */ |
| 1721 | static final int INVALIDATED = 0x80000000; |
| 1722 | |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1723 | /* Masks for mPrivateFlags2 */ |
| 1724 | |
| 1725 | /** |
| 1726 | * Indicates that this view has reported that it can accept the current drag's content. |
| 1727 | * Cleared when the drag operation concludes. |
| 1728 | * @hide |
| 1729 | */ |
| 1730 | static final int DRAG_CAN_ACCEPT = 0x00000001; |
| 1731 | |
| 1732 | /** |
| 1733 | * Indicates that this view is currently directly under the drag location in a |
| 1734 | * drag-and-drop operation involving content that it can accept. Cleared when |
| 1735 | * the drag exits the view, or when the drag operation concludes. |
| 1736 | * @hide |
| 1737 | */ |
| 1738 | static final int DRAG_HOVERED = 0x00000002; |
| 1739 | |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 1740 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 1741 | * Indicates whether the view layout direction has been resolved and drawn to the |
| 1742 | * right-to-left direction. |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 1743 | * |
| 1744 | * @hide |
| 1745 | */ |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 1746 | static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004; |
| 1747 | |
| 1748 | /** |
| 1749 | * Indicates whether the view layout direction has been resolved. |
| 1750 | * |
| 1751 | * @hide |
| 1752 | */ |
| 1753 | static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008; |
| 1754 | |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 1755 | |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1756 | /* End of masks for mPrivateFlags2 */ |
| 1757 | |
| 1758 | static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED; |
| 1759 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1760 | /** |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 1761 | * Always allow a user to over-scroll this view, provided it is a |
| 1762 | * view that can scroll. |
| 1763 | * |
| 1764 | * @see #getOverScrollMode() |
| 1765 | * @see #setOverScrollMode(int) |
| 1766 | */ |
| 1767 | public static final int OVER_SCROLL_ALWAYS = 0; |
| 1768 | |
| 1769 | /** |
| 1770 | * Allow a user to over-scroll this view only if the content is large |
| 1771 | * enough to meaningfully scroll, provided it is a view that can scroll. |
| 1772 | * |
| 1773 | * @see #getOverScrollMode() |
| 1774 | * @see #setOverScrollMode(int) |
| 1775 | */ |
| 1776 | public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1; |
| 1777 | |
| 1778 | /** |
| 1779 | * Never allow a user to over-scroll this view. |
| 1780 | * |
| 1781 | * @see #getOverScrollMode() |
| 1782 | * @see #setOverScrollMode(int) |
| 1783 | */ |
| 1784 | public static final int OVER_SCROLL_NEVER = 2; |
| 1785 | |
| 1786 | /** |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1787 | * View has requested the status bar to be visible (the default). |
| 1788 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1789 | * @see #setSystemUiVisibility(int) |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1790 | */ |
| 1791 | public static final int STATUS_BAR_VISIBLE = 0; |
| 1792 | |
| 1793 | /** |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1794 | * View has requested the status bar to be hidden. |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1795 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 1796 | * @see #setSystemUiVisibility(int) |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1797 | */ |
| 1798 | public static final int STATUS_BAR_HIDDEN = 0x00000001; |
| 1799 | |
| 1800 | /** |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 1801 | * @hide |
| 1802 | * |
| 1803 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1804 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1805 | * |
| 1806 | * Flag to make the status bar not expandable. Unless you also |
| 1807 | * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show. |
| 1808 | */ |
| 1809 | public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000; |
| 1810 | |
| 1811 | /** |
| 1812 | * @hide |
| 1813 | * |
| 1814 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1815 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1816 | * |
| 1817 | * Flag to hide notification icons and scrolling ticker text. |
| 1818 | */ |
| 1819 | public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000; |
| 1820 | |
| 1821 | /** |
| 1822 | * @hide |
| 1823 | * |
| 1824 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1825 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1826 | * |
| 1827 | * Flag to disable incoming notification alerts. This will not block |
| 1828 | * icons, but it will block sound, vibrating and other visual or aural notifications. |
| 1829 | */ |
| 1830 | public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000; |
| 1831 | |
| 1832 | /** |
| 1833 | * @hide |
| 1834 | * |
| 1835 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1836 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1837 | * |
| 1838 | * Flag to hide only the scrolling ticker. Note that |
| 1839 | * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies |
| 1840 | * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}. |
| 1841 | */ |
| 1842 | public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000; |
| 1843 | |
| 1844 | /** |
| 1845 | * @hide |
| 1846 | * |
| 1847 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1848 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1849 | * |
| 1850 | * Flag to hide the center system info area. |
| 1851 | */ |
| 1852 | public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000; |
| 1853 | |
| 1854 | /** |
| 1855 | * @hide |
| 1856 | * |
| 1857 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1858 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1859 | * |
| 1860 | * Flag to hide only the navigation buttons. Don't use this |
| 1861 | * unless you're a special part of the system UI (i.e., setup wizard, keyguard). |
Joe Onorato | 6478adc | 2011-01-27 21:15:01 -0800 | [diff] [blame] | 1862 | * |
| 1863 | * THIS DOES NOT DISABLE THE BACK BUTTON |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 1864 | */ |
| 1865 | public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000; |
| 1866 | |
| 1867 | /** |
| 1868 | * @hide |
| 1869 | * |
| 1870 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1871 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1872 | * |
Joe Onorato | 6478adc | 2011-01-27 21:15:01 -0800 | [diff] [blame] | 1873 | * Flag to hide only the back button. Don't use this |
| 1874 | * unless you're a special part of the system UI (i.e., setup wizard, keyguard). |
| 1875 | */ |
| 1876 | public static final int STATUS_BAR_DISABLE_BACK = 0x00400000; |
| 1877 | |
| 1878 | /** |
| 1879 | * @hide |
| 1880 | * |
| 1881 | * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked |
| 1882 | * out of the public fields to keep the undefined bits out of the developer's way. |
| 1883 | * |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 1884 | * Flag to hide only the clock. You might use this if your activity has |
| 1885 | * its own clock making the status bar's clock redundant. |
| 1886 | */ |
Joe Onorato | 6478adc | 2011-01-27 21:15:01 -0800 | [diff] [blame] | 1887 | public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000; |
| 1888 | |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 1889 | /** |
| 1890 | * @hide |
| 1891 | */ |
| 1892 | public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN; |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 1893 | |
| 1894 | /** |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 1895 | * Controls the over-scroll mode for this view. |
| 1896 | * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)}, |
| 1897 | * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}, |
| 1898 | * and {@link #OVER_SCROLL_NEVER}. |
| 1899 | */ |
| 1900 | private int mOverScrollMode; |
| 1901 | |
| 1902 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | * The parent this view is attached to. |
| 1904 | * {@hide} |
| 1905 | * |
| 1906 | * @see #getParent() |
| 1907 | */ |
| 1908 | protected ViewParent mParent; |
| 1909 | |
| 1910 | /** |
| 1911 | * {@hide} |
| 1912 | */ |
| 1913 | AttachInfo mAttachInfo; |
| 1914 | |
| 1915 | /** |
| 1916 | * {@hide} |
| 1917 | */ |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1918 | @ViewDebug.ExportedProperty(flagMapping = { |
| 1919 | @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT, |
| 1920 | name = "FORCE_LAYOUT"), |
| 1921 | @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED, |
| 1922 | name = "LAYOUT_REQUIRED"), |
| 1923 | @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID, |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1924 | name = "DRAWING_CACHE_INVALID", outputIf = false), |
Romain Guy | 809a7f6 | 2009-05-14 15:44:42 -0700 | [diff] [blame] | 1925 | @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true), |
| 1926 | @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false), |
| 1927 | @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"), |
| 1928 | @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY") |
| 1929 | }) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | int mPrivateFlags; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1931 | int mPrivateFlags2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | |
| 1933 | /** |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1934 | * This view's request for the visibility of the status bar. |
| 1935 | * @hide |
| 1936 | */ |
Joe Onorato | ac0ee89 | 2011-01-30 15:38:30 -0800 | [diff] [blame] | 1937 | @ViewDebug.ExportedProperty() |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1938 | int mSystemUiVisibility; |
| 1939 | |
| 1940 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | * Count of how many windows this view has been attached to. |
| 1942 | */ |
| 1943 | int mWindowAttachCount; |
| 1944 | |
| 1945 | /** |
| 1946 | * The layout parameters associated with this view and used by the parent |
| 1947 | * {@link android.view.ViewGroup} to determine how this view should be |
| 1948 | * laid out. |
| 1949 | * {@hide} |
| 1950 | */ |
| 1951 | protected ViewGroup.LayoutParams mLayoutParams; |
| 1952 | |
| 1953 | /** |
| 1954 | * The view flags hold various views states. |
| 1955 | * {@hide} |
| 1956 | */ |
| 1957 | @ViewDebug.ExportedProperty |
| 1958 | int mViewFlags; |
| 1959 | |
| 1960 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1961 | * The transform matrix for the View. This transform is calculated internally |
| 1962 | * based on the rotation, scaleX, and scaleY properties. The identity matrix |
| 1963 | * is used by default. Do *not* use this variable directly; instead call |
| 1964 | * getMatrix(), which will automatically recalculate the matrix if necessary |
| 1965 | * to get the correct matrix based on the latest rotation and scale properties. |
| 1966 | */ |
| 1967 | private final Matrix mMatrix = new Matrix(); |
| 1968 | |
| 1969 | /** |
| 1970 | * The transform matrix for the View. This transform is calculated internally |
| 1971 | * based on the rotation, scaleX, and scaleY properties. The identity matrix |
| 1972 | * is used by default. Do *not* use this variable directly; instead call |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 1973 | * getInverseMatrix(), which will automatically recalculate the matrix if necessary |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1974 | * to get the correct matrix based on the latest rotation and scale properties. |
| 1975 | */ |
| 1976 | private Matrix mInverseMatrix; |
| 1977 | |
| 1978 | /** |
| 1979 | * An internal variable that tracks whether we need to recalculate the |
| 1980 | * transform matrix, based on whether the rotation or scaleX/Y properties |
| 1981 | * have changed since the matrix was last calculated. |
| 1982 | */ |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1983 | boolean mMatrixDirty = false; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1984 | |
| 1985 | /** |
| 1986 | * An internal variable that tracks whether we need to recalculate the |
| 1987 | * transform matrix, based on whether the rotation or scaleX/Y properties |
| 1988 | * have changed since the matrix was last calculated. |
| 1989 | */ |
| 1990 | private boolean mInverseMatrixDirty = true; |
| 1991 | |
| 1992 | /** |
| 1993 | * A variable that tracks whether we need to recalculate the |
| 1994 | * transform matrix, based on whether the rotation or scaleX/Y properties |
| 1995 | * have changed since the matrix was last calculated. This variable |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 1996 | * is only valid after a call to updateMatrix() or to a function that |
| 1997 | * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix(). |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1998 | */ |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1999 | private boolean mMatrixIsIdentity = true; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2000 | |
| 2001 | /** |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 2002 | * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set. |
| 2003 | */ |
| 2004 | private Camera mCamera = null; |
| 2005 | |
| 2006 | /** |
| 2007 | * This matrix is used when computing the matrix for 3D rotations. |
| 2008 | */ |
| 2009 | private Matrix matrix3D = null; |
| 2010 | |
| 2011 | /** |
| 2012 | * These prev values are used to recalculate a centered pivot point when necessary. The |
| 2013 | * pivot point is only used in matrix operations (when rotation, scale, or translation are |
| 2014 | * set), so thes values are only used then as well. |
| 2015 | */ |
| 2016 | private int mPrevWidth = -1; |
| 2017 | private int mPrevHeight = -1; |
| 2018 | |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2019 | private boolean mLastIsOpaque; |
| 2020 | |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 2021 | /** |
| 2022 | * Convenience value to check for float values that are close enough to zero to be considered |
| 2023 | * zero. |
| 2024 | */ |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 2025 | private static final float NONZERO_EPSILON = .001f; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 2026 | |
| 2027 | /** |
| 2028 | * The degrees rotation around the vertical axis through the pivot point. |
| 2029 | */ |
| 2030 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2031 | float mRotationY = 0f; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 2032 | |
| 2033 | /** |
| 2034 | * The degrees rotation around the horizontal axis through the pivot point. |
| 2035 | */ |
| 2036 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2037 | float mRotationX = 0f; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 2038 | |
| 2039 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2040 | * The degrees rotation around the pivot point. |
| 2041 | */ |
| 2042 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2043 | float mRotation = 0f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2044 | |
| 2045 | /** |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 2046 | * The amount of translation of the object away from its left property (post-layout). |
| 2047 | */ |
| 2048 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2049 | float mTranslationX = 0f; |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 2050 | |
| 2051 | /** |
| 2052 | * The amount of translation of the object away from its top property (post-layout). |
| 2053 | */ |
| 2054 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2055 | float mTranslationY = 0f; |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 2056 | |
| 2057 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2058 | * The amount of scale in the x direction around the pivot point. A |
| 2059 | * value of 1 means no scaling is applied. |
| 2060 | */ |
| 2061 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2062 | float mScaleX = 1f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2063 | |
| 2064 | /** |
| 2065 | * The amount of scale in the y direction around the pivot point. A |
| 2066 | * value of 1 means no scaling is applied. |
| 2067 | */ |
| 2068 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2069 | float mScaleY = 1f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2070 | |
| 2071 | /** |
| 2072 | * The amount of scale in the x direction around the pivot point. A |
| 2073 | * value of 1 means no scaling is applied. |
| 2074 | */ |
| 2075 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2076 | float mPivotX = 0f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2077 | |
| 2078 | /** |
| 2079 | * The amount of scale in the y direction around the pivot point. A |
| 2080 | * value of 1 means no scaling is applied. |
| 2081 | */ |
| 2082 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2083 | float mPivotY = 0f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2084 | |
| 2085 | /** |
| 2086 | * The opacity of the View. This is a value from 0 to 1, where 0 means |
| 2087 | * completely transparent and 1 means completely opaque. |
| 2088 | */ |
| 2089 | @ViewDebug.ExportedProperty |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2090 | float mAlpha = 1f; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2091 | |
| 2092 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | * The distance in pixels from the left edge of this view's parent |
| 2094 | * to the left edge of this view. |
| 2095 | * {@hide} |
| 2096 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2097 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2098 | protected int mLeft; |
| 2099 | /** |
| 2100 | * The distance in pixels from the left edge of this view's parent |
| 2101 | * to the right edge of this view. |
| 2102 | * {@hide} |
| 2103 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2104 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2105 | protected int mRight; |
| 2106 | /** |
| 2107 | * The distance in pixels from the top edge of this view's parent |
| 2108 | * to the top edge of this view. |
| 2109 | * {@hide} |
| 2110 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2111 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | protected int mTop; |
| 2113 | /** |
| 2114 | * The distance in pixels from the top edge of this view's parent |
| 2115 | * to the bottom edge of this view. |
| 2116 | * {@hide} |
| 2117 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2118 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2119 | protected int mBottom; |
| 2120 | |
| 2121 | /** |
| 2122 | * The offset, in pixels, by which the content of this view is scrolled |
| 2123 | * horizontally. |
| 2124 | * {@hide} |
| 2125 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2126 | @ViewDebug.ExportedProperty(category = "scrolling") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | protected int mScrollX; |
| 2128 | /** |
| 2129 | * The offset, in pixels, by which the content of this view is scrolled |
| 2130 | * vertically. |
| 2131 | * {@hide} |
| 2132 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2133 | @ViewDebug.ExportedProperty(category = "scrolling") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2134 | protected int mScrollY; |
| 2135 | |
| 2136 | /** |
| 2137 | * The left padding in pixels, that is the distance in pixels between the |
| 2138 | * left edge of this view and the left edge of its content. |
| 2139 | * {@hide} |
| 2140 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2141 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | protected int mPaddingLeft; |
| 2143 | /** |
| 2144 | * The right padding in pixels, that is the distance in pixels between the |
| 2145 | * right edge of this view and the right edge of its content. |
| 2146 | * {@hide} |
| 2147 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2148 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2149 | protected int mPaddingRight; |
| 2150 | /** |
| 2151 | * The top padding in pixels, that is the distance in pixels between the |
| 2152 | * top edge of this view and the top edge of its content. |
| 2153 | * {@hide} |
| 2154 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2155 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2156 | protected int mPaddingTop; |
| 2157 | /** |
| 2158 | * The bottom padding in pixels, that is the distance in pixels between the |
| 2159 | * bottom edge of this view and the bottom edge of its content. |
| 2160 | * {@hide} |
| 2161 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2162 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2163 | protected int mPaddingBottom; |
| 2164 | |
| 2165 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2166 | * Briefly describes the view and is primarily used for accessibility support. |
| 2167 | */ |
| 2168 | private CharSequence mContentDescription; |
| 2169 | |
| 2170 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2171 | * Cache the paddingRight set by the user to append to the scrollbar's size. |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2172 | * |
| 2173 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2174 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2175 | @ViewDebug.ExportedProperty(category = "padding") |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2176 | protected int mUserPaddingRight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2177 | |
| 2178 | /** |
| 2179 | * Cache the paddingBottom set by the user to append to the scrollbar's size. |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2180 | * |
| 2181 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2183 | @ViewDebug.ExportedProperty(category = "padding") |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2184 | protected int mUserPaddingBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2185 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2186 | /** |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 2187 | * Cache the paddingLeft set by the user to append to the scrollbar's size. |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2188 | * |
| 2189 | * @hide |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 2190 | */ |
| 2191 | @ViewDebug.ExportedProperty(category = "padding") |
Fabrice Di Meglio | 54d6962 | 2011-07-15 16:46:44 -0700 | [diff] [blame] | 2192 | protected int mUserPaddingLeft; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 2193 | |
| 2194 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 2195 | * Cache if the user padding is relative. |
| 2196 | * |
| 2197 | */ |
| 2198 | @ViewDebug.ExportedProperty(category = "padding") |
| 2199 | boolean mUserPaddingRelative; |
| 2200 | |
| 2201 | /** |
| 2202 | * Cache the paddingStart set by the user to append to the scrollbar's size. |
| 2203 | * |
| 2204 | */ |
| 2205 | @ViewDebug.ExportedProperty(category = "padding") |
| 2206 | int mUserPaddingStart; |
| 2207 | |
| 2208 | /** |
| 2209 | * Cache the paddingEnd set by the user to append to the scrollbar's size. |
| 2210 | * |
| 2211 | */ |
| 2212 | @ViewDebug.ExportedProperty(category = "padding") |
| 2213 | int mUserPaddingEnd; |
| 2214 | |
| 2215 | /** |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 2216 | * @hide |
| 2217 | */ |
| 2218 | int mOldWidthMeasureSpec = Integer.MIN_VALUE; |
| 2219 | /** |
| 2220 | * @hide |
| 2221 | */ |
| 2222 | int mOldHeightMeasureSpec = Integer.MIN_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2223 | |
| 2224 | private Resources mResources = null; |
| 2225 | |
| 2226 | private Drawable mBGDrawable; |
| 2227 | |
| 2228 | private int mBackgroundResource; |
| 2229 | private boolean mBackgroundSizeChanged; |
| 2230 | |
| 2231 | /** |
| 2232 | * Listener used to dispatch focus change events. |
| 2233 | * This field should be made private, so it is hidden from the SDK. |
| 2234 | * {@hide} |
| 2235 | */ |
| 2236 | protected OnFocusChangeListener mOnFocusChangeListener; |
| 2237 | |
| 2238 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2239 | * Listeners for layout change events. |
| 2240 | */ |
| 2241 | private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners; |
| 2242 | |
| 2243 | /** |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 2244 | * Listeners for attach events. |
| 2245 | */ |
| 2246 | private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners; |
| 2247 | |
| 2248 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2249 | * Listener used to dispatch click events. |
| 2250 | * This field should be made private, so it is hidden from the SDK. |
| 2251 | * {@hide} |
| 2252 | */ |
| 2253 | protected OnClickListener mOnClickListener; |
| 2254 | |
| 2255 | /** |
| 2256 | * Listener used to dispatch long click events. |
| 2257 | * This field should be made private, so it is hidden from the SDK. |
| 2258 | * {@hide} |
| 2259 | */ |
| 2260 | protected OnLongClickListener mOnLongClickListener; |
| 2261 | |
| 2262 | /** |
| 2263 | * Listener used to build the context menu. |
| 2264 | * This field should be made private, so it is hidden from the SDK. |
| 2265 | * {@hide} |
| 2266 | */ |
| 2267 | protected OnCreateContextMenuListener mOnCreateContextMenuListener; |
| 2268 | |
| 2269 | private OnKeyListener mOnKeyListener; |
| 2270 | |
| 2271 | private OnTouchListener mOnTouchListener; |
| 2272 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2273 | private OnHoverListener mOnHoverListener; |
| 2274 | |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2275 | private OnGenericMotionListener mOnGenericMotionListener; |
| 2276 | |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 2277 | private OnDragListener mOnDragListener; |
| 2278 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 2279 | private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener; |
| 2280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2281 | /** |
| 2282 | * The application environment this view lives in. |
| 2283 | * This field should be made private, so it is hidden from the SDK. |
| 2284 | * {@hide} |
| 2285 | */ |
| 2286 | protected Context mContext; |
| 2287 | |
| 2288 | private ScrollabilityCache mScrollCache; |
| 2289 | |
| 2290 | private int[] mDrawableState = null; |
| 2291 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 2292 | /** |
| 2293 | * Set to true when drawing cache is enabled and cannot be created. |
| 2294 | * |
| 2295 | * @hide |
| 2296 | */ |
| 2297 | public boolean mCachingFailed; |
| 2298 | |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 2299 | private Bitmap mDrawingCache; |
| 2300 | private Bitmap mUnscaledDrawingCache; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2301 | private HardwareLayer mHardwareLayer; |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 2302 | DisplayList mDisplayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2303 | |
| 2304 | /** |
| 2305 | * When this view has focus and the next focus is {@link #FOCUS_LEFT}, |
| 2306 | * the user may specify which view to go to next. |
| 2307 | */ |
| 2308 | private int mNextFocusLeftId = View.NO_ID; |
| 2309 | |
| 2310 | /** |
| 2311 | * When this view has focus and the next focus is {@link #FOCUS_RIGHT}, |
| 2312 | * the user may specify which view to go to next. |
| 2313 | */ |
| 2314 | private int mNextFocusRightId = View.NO_ID; |
| 2315 | |
| 2316 | /** |
| 2317 | * When this view has focus and the next focus is {@link #FOCUS_UP}, |
| 2318 | * the user may specify which view to go to next. |
| 2319 | */ |
| 2320 | private int mNextFocusUpId = View.NO_ID; |
| 2321 | |
| 2322 | /** |
| 2323 | * When this view has focus and the next focus is {@link #FOCUS_DOWN}, |
| 2324 | * the user may specify which view to go to next. |
| 2325 | */ |
| 2326 | private int mNextFocusDownId = View.NO_ID; |
| 2327 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2328 | /** |
| 2329 | * When this view has focus and the next focus is {@link #FOCUS_FORWARD}, |
| 2330 | * the user may specify which view to go to next. |
| 2331 | */ |
| 2332 | int mNextFocusForwardId = View.NO_ID; |
| 2333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2334 | private CheckForLongPress mPendingCheckForLongPress; |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 2335 | private CheckForTap mPendingCheckForTap = null; |
Adam Powell | a35d768 | 2010-03-12 14:48:13 -0800 | [diff] [blame] | 2336 | private PerformClick mPerformClick; |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 2337 | private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | private UnsetPressedState mUnsetPressedState; |
| 2340 | |
| 2341 | /** |
| 2342 | * Whether the long press's action has been invoked. The tap's action is invoked on the |
| 2343 | * up event while a long press is invoked as soon as the long press duration is reached, so |
| 2344 | * a long press could be performed before the tap is checked, in which case the tap's action |
| 2345 | * should not be invoked. |
| 2346 | */ |
| 2347 | private boolean mHasPerformedLongPress; |
| 2348 | |
| 2349 | /** |
| 2350 | * The minimum height of the view. We'll try our best to have the height |
| 2351 | * of this view to at least this amount. |
| 2352 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2353 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2354 | private int mMinHeight; |
| 2355 | |
| 2356 | /** |
| 2357 | * The minimum width of the view. We'll try our best to have the width |
| 2358 | * of this view to at least this amount. |
| 2359 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 2360 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2361 | private int mMinWidth; |
| 2362 | |
| 2363 | /** |
| 2364 | * The delegate to handle touch events that are physically in this view |
| 2365 | * but should be handled by another view. |
| 2366 | */ |
| 2367 | private TouchDelegate mTouchDelegate = null; |
| 2368 | |
| 2369 | /** |
| 2370 | * Solid color to use as a background when creating the drawing cache. Enables |
| 2371 | * the cache to use 16 bit bitmaps instead of 32 bit. |
| 2372 | */ |
| 2373 | private int mDrawingCacheBackgroundColor = 0; |
| 2374 | |
| 2375 | /** |
| 2376 | * Special tree observer used when mAttachInfo is null. |
| 2377 | */ |
| 2378 | private ViewTreeObserver mFloatingTreeObserver; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2379 | |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 2380 | /** |
| 2381 | * Cache the touch slop from the context that created the view. |
| 2382 | */ |
| 2383 | private int mTouchSlop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2385 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 2386 | * Object that handles automatic animation of view properties. |
| 2387 | */ |
| 2388 | private ViewPropertyAnimator mAnimator = null; |
| 2389 | |
| 2390 | /** |
Christopher Tate | 251602f | 2011-01-28 17:54:12 -0800 | [diff] [blame] | 2391 | * Flag indicating that a drag can cross window boundaries. When |
| 2392 | * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called |
| 2393 | * with this flag set, all visible applications will be able to participate |
| 2394 | * in the drag operation and receive the dragged content. |
Christopher Tate | 7f9ff9d | 2011-02-14 17:31:13 -0800 | [diff] [blame] | 2395 | * |
| 2396 | * @hide |
Christopher Tate | 02d2b3b | 2011-01-10 20:43:53 -0800 | [diff] [blame] | 2397 | */ |
| 2398 | public static final int DRAG_FLAG_GLOBAL = 1; |
| 2399 | |
| 2400 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2401 | * Vertical scroll factor cached by {@link #getVerticalScrollFactor}. |
| 2402 | */ |
| 2403 | private float mVerticalScrollFactor; |
| 2404 | |
| 2405 | /** |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 2406 | * Position of the vertical scroll bar. |
| 2407 | */ |
| 2408 | private int mVerticalScrollbarPosition; |
| 2409 | |
| 2410 | /** |
| 2411 | * Position the scroll bar at the default position as determined by the system. |
| 2412 | */ |
| 2413 | public static final int SCROLLBAR_POSITION_DEFAULT = 0; |
| 2414 | |
| 2415 | /** |
| 2416 | * Position the scroll bar along the left edge. |
| 2417 | */ |
| 2418 | public static final int SCROLLBAR_POSITION_LEFT = 1; |
| 2419 | |
| 2420 | /** |
| 2421 | * Position the scroll bar along the right edge. |
| 2422 | */ |
| 2423 | public static final int SCROLLBAR_POSITION_RIGHT = 2; |
| 2424 | |
| 2425 | /** |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2426 | * Indicates that the view does not have a layer. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2427 | * |
| 2428 | * @see #getLayerType() |
| 2429 | * @see #setLayerType(int, android.graphics.Paint) |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2430 | * @see #LAYER_TYPE_SOFTWARE |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2431 | * @see #LAYER_TYPE_HARDWARE |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2432 | */ |
| 2433 | public static final int LAYER_TYPE_NONE = 0; |
| 2434 | |
| 2435 | /** |
| 2436 | * <p>Indicates that the view has a software layer. A software layer is backed |
| 2437 | * by a bitmap and causes the view to be rendered using Android's software |
| 2438 | * rendering pipeline, even if hardware acceleration is enabled.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2439 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2440 | * <p>Software layers have various usages:</p> |
| 2441 | * <p>When the application is not using hardware acceleration, a software layer |
| 2442 | * is useful to apply a specific color filter and/or blending mode and/or |
| 2443 | * translucency to a view and all its children.</p> |
| 2444 | * <p>When the application is using hardware acceleration, a software layer |
| 2445 | * is useful to render drawing primitives not supported by the hardware |
| 2446 | * accelerated pipeline. It can also be used to cache a complex view tree |
| 2447 | * into a texture and reduce the complexity of drawing operations. For instance, |
| 2448 | * when animating a complex view tree with a translation, a software layer can |
| 2449 | * be used to render the view tree only once.</p> |
| 2450 | * <p>Software layers should be avoided when the affected view tree updates |
| 2451 | * often. Every update will require to re-render the software layer, which can |
| 2452 | * potentially be slow (particularly when hardware acceleration is turned on |
| 2453 | * since the layer will have to be uploaded into a hardware texture after every |
| 2454 | * update.)</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2455 | * |
| 2456 | * @see #getLayerType() |
| 2457 | * @see #setLayerType(int, android.graphics.Paint) |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2458 | * @see #LAYER_TYPE_NONE |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2459 | * @see #LAYER_TYPE_HARDWARE |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2460 | */ |
| 2461 | public static final int LAYER_TYPE_SOFTWARE = 1; |
| 2462 | |
| 2463 | /** |
| 2464 | * <p>Indicates that the view has a hardware layer. A hardware layer is backed |
| 2465 | * by a hardware specific texture (generally Frame Buffer Objects or FBO on |
| 2466 | * OpenGL hardware) and causes the view to be rendered using Android's hardware |
| 2467 | * rendering pipeline, but only if hardware acceleration is turned on for the |
| 2468 | * view hierarchy. When hardware acceleration is turned off, hardware layers |
| 2469 | * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2470 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2471 | * <p>A hardware layer is useful to apply a specific color filter and/or |
| 2472 | * blending mode and/or translucency to a view and all its children.</p> |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2473 | * <p>A hardware layer can be used to cache a complex view tree into a |
| 2474 | * texture and reduce the complexity of drawing operations. For instance, |
| 2475 | * when animating a complex view tree with a translation, a hardware layer can |
| 2476 | * be used to render the view tree only once.</p> |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2477 | * <p>A hardware layer can also be used to increase the rendering quality when |
| 2478 | * rotation transformations are applied on a view. It can also be used to |
| 2479 | * prevent potential clipping issues when applying 3D transforms on a view.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2480 | * |
| 2481 | * @see #getLayerType() |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2482 | * @see #setLayerType(int, android.graphics.Paint) |
| 2483 | * @see #LAYER_TYPE_NONE |
| 2484 | * @see #LAYER_TYPE_SOFTWARE |
| 2485 | */ |
| 2486 | public static final int LAYER_TYPE_HARDWARE = 2; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2487 | |
Romain Guy | 3aaff3a | 2011-01-12 14:18:47 -0800 | [diff] [blame] | 2488 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
| 2489 | @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"), |
| 2490 | @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"), |
| 2491 | @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE") |
| 2492 | }) |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2493 | int mLayerType = LAYER_TYPE_NONE; |
| 2494 | Paint mLayerPaint; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 2495 | Rect mLocalDirtyRect; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2496 | |
| 2497 | /** |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 2498 | * Set to true when the view is sending hover accessibility events because it |
| 2499 | * is the innermost hovered view. |
| 2500 | */ |
| 2501 | private boolean mSendingHoverAccessibilityEvents; |
| 2502 | |
| 2503 | /** |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2504 | * Text direction is inherited thru {@link ViewGroup} |
| 2505 | * @hide |
| 2506 | */ |
| 2507 | public static final int TEXT_DIRECTION_INHERIT = 0; |
| 2508 | |
| 2509 | /** |
| 2510 | * Text direction is using "first strong algorithm". The first strong directional character |
| 2511 | * determines the paragraph direction. If there is no strong directional character, the |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2512 | * paragraph direction is the view's resolved ayout direction. |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2513 | * |
| 2514 | * @hide |
| 2515 | */ |
| 2516 | public static final int TEXT_DIRECTION_FIRST_STRONG = 1; |
| 2517 | |
| 2518 | /** |
| 2519 | * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains |
| 2520 | * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2521 | * If there are neither, the paragraph direction is the view's resolved layout direction. |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2522 | * |
| 2523 | * @hide |
| 2524 | */ |
| 2525 | public static final int TEXT_DIRECTION_ANY_RTL = 2; |
| 2526 | |
| 2527 | /** |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 2528 | * Text direction is the same as the one held by a 60% majority of the characters. If there is |
| 2529 | * no majority then the paragraph direction is the resolved layout direction of the View. |
| 2530 | * |
| 2531 | * @hide |
| 2532 | */ |
| 2533 | public static final int TEXT_DIRECTION_CHAR_COUNT = 3; |
| 2534 | |
| 2535 | /** |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2536 | * Text direction is forced to LTR. |
| 2537 | * |
| 2538 | * @hide |
| 2539 | */ |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 2540 | public static final int TEXT_DIRECTION_LTR = 4; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2541 | |
| 2542 | /** |
| 2543 | * Text direction is forced to RTL. |
| 2544 | * |
| 2545 | * @hide |
| 2546 | */ |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 2547 | public static final int TEXT_DIRECTION_RTL = 5; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2548 | |
| 2549 | /** |
| 2550 | * Default text direction is inherited |
| 2551 | */ |
| 2552 | protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT; |
| 2553 | |
| 2554 | /** |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 2555 | * Default threshold for "char count" heuristic. |
| 2556 | */ |
| 2557 | protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f; |
| 2558 | |
| 2559 | /** |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2560 | * The text direction that has been defined by {@link #setTextDirection(int)}. |
| 2561 | * |
| 2562 | * {@hide} |
| 2563 | */ |
| 2564 | @ViewDebug.ExportedProperty(category = "text", mapping = { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2565 | @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"), |
| 2566 | @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"), |
| 2567 | @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"), |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 2568 | @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"), |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2569 | @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"), |
| 2570 | @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL") |
| 2571 | }) |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2572 | private int mTextDirection = DEFAULT_TEXT_DIRECTION; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2573 | |
| 2574 | /** |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2575 | * The resolved text direction. This needs resolution if the value is |
| 2576 | * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is |
| 2577 | * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent |
| 2578 | * chain of the view. |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2579 | * |
| 2580 | * {@hide} |
| 2581 | */ |
| 2582 | @ViewDebug.ExportedProperty(category = "text", mapping = { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2583 | @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"), |
| 2584 | @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"), |
| 2585 | @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"), |
| 2586 | @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"), |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2587 | @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"), |
| 2588 | @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL") |
| 2589 | }) |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 2590 | private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2591 | |
| 2592 | /** |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 2593 | * Consistency verifier for debugging purposes. |
| 2594 | * @hide |
| 2595 | */ |
| 2596 | protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier = |
| 2597 | InputEventConsistencyVerifier.isInstrumentationEnabled() ? |
| 2598 | new InputEventConsistencyVerifier(this, 0) : null; |
| 2599 | |
| 2600 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2601 | * Simple constructor to use when creating a view from code. |
| 2602 | * |
| 2603 | * @param context The Context the view is running in, through which it can |
| 2604 | * access the current theme, resources, etc. |
| 2605 | */ |
| 2606 | public View(Context context) { |
| 2607 | mContext = context; |
| 2608 | mResources = context != null ? context.getResources() : null; |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2609 | mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT; |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 2610 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 2611 | setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS); |
Fabrice Di Meglio | f9e3650 | 2011-06-21 18:41:48 -0700 | [diff] [blame] | 2612 | mUserPaddingStart = -1; |
| 2613 | mUserPaddingEnd = -1; |
| 2614 | mUserPaddingRelative = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * Constructor that is called when inflating a view from XML. This is called |
| 2619 | * when a view is being constructed from an XML file, supplying attributes |
| 2620 | * that were specified in the XML file. This version uses a default style of |
| 2621 | * 0, so the only attribute values applied are those in the Context's Theme |
| 2622 | * and the given AttributeSet. |
| 2623 | * |
| 2624 | * <p> |
| 2625 | * The method onFinishInflate() will be called after all children have been |
| 2626 | * added. |
| 2627 | * |
| 2628 | * @param context The Context the view is running in, through which it can |
| 2629 | * access the current theme, resources, etc. |
| 2630 | * @param attrs The attributes of the XML tag that is inflating the view. |
| 2631 | * @see #View(Context, AttributeSet, int) |
| 2632 | */ |
| 2633 | public View(Context context, AttributeSet attrs) { |
| 2634 | this(context, attrs, 0); |
| 2635 | } |
| 2636 | |
| 2637 | /** |
| 2638 | * Perform inflation from XML and apply a class-specific base style. This |
| 2639 | * constructor of View allows subclasses to use their own base style when |
| 2640 | * they are inflating. For example, a Button class's constructor would call |
| 2641 | * this version of the super class constructor and supply |
| 2642 | * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows |
| 2643 | * the theme's button style to modify all of the base view attributes (in |
| 2644 | * particular its background) as well as the Button class's attributes. |
| 2645 | * |
| 2646 | * @param context The Context the view is running in, through which it can |
| 2647 | * access the current theme, resources, etc. |
| 2648 | * @param attrs The attributes of the XML tag that is inflating the view. |
| 2649 | * @param defStyle The default style to apply to this view. If 0, no style |
| 2650 | * will be applied (beyond what is included in the theme). This may |
| 2651 | * either be an attribute resource, whose value will be retrieved |
| 2652 | * from the current theme, or an explicit style resource. |
| 2653 | * @see #View(Context, AttributeSet) |
| 2654 | */ |
| 2655 | public View(Context context, AttributeSet attrs, int defStyle) { |
| 2656 | this(context); |
| 2657 | |
| 2658 | TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, |
| 2659 | defStyle, 0); |
| 2660 | |
| 2661 | Drawable background = null; |
| 2662 | |
| 2663 | int leftPadding = -1; |
| 2664 | int topPadding = -1; |
| 2665 | int rightPadding = -1; |
| 2666 | int bottomPadding = -1; |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 2667 | int startPadding = -1; |
| 2668 | int endPadding = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2669 | |
| 2670 | int padding = -1; |
| 2671 | |
| 2672 | int viewFlagValues = 0; |
| 2673 | int viewFlagMasks = 0; |
| 2674 | |
| 2675 | boolean setScrollContainer = false; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2676 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | int x = 0; |
| 2678 | int y = 0; |
| 2679 | |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 2680 | float tx = 0; |
| 2681 | float ty = 0; |
| 2682 | float rotation = 0; |
| 2683 | float rotationX = 0; |
| 2684 | float rotationY = 0; |
| 2685 | float sx = 1f; |
| 2686 | float sy = 1f; |
| 2687 | boolean transformSet = false; |
| 2688 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2689 | int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY; |
| 2690 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 2691 | int overScrollMode = mOverScrollMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2692 | final int N = a.getIndexCount(); |
| 2693 | for (int i = 0; i < N; i++) { |
| 2694 | int attr = a.getIndex(i); |
| 2695 | switch (attr) { |
| 2696 | case com.android.internal.R.styleable.View_background: |
| 2697 | background = a.getDrawable(attr); |
| 2698 | break; |
| 2699 | case com.android.internal.R.styleable.View_padding: |
| 2700 | padding = a.getDimensionPixelSize(attr, -1); |
| 2701 | break; |
| 2702 | case com.android.internal.R.styleable.View_paddingLeft: |
| 2703 | leftPadding = a.getDimensionPixelSize(attr, -1); |
| 2704 | break; |
| 2705 | case com.android.internal.R.styleable.View_paddingTop: |
| 2706 | topPadding = a.getDimensionPixelSize(attr, -1); |
| 2707 | break; |
| 2708 | case com.android.internal.R.styleable.View_paddingRight: |
| 2709 | rightPadding = a.getDimensionPixelSize(attr, -1); |
| 2710 | break; |
| 2711 | case com.android.internal.R.styleable.View_paddingBottom: |
| 2712 | bottomPadding = a.getDimensionPixelSize(attr, -1); |
| 2713 | break; |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 2714 | case com.android.internal.R.styleable.View_paddingStart: |
| 2715 | startPadding = a.getDimensionPixelSize(attr, -1); |
| 2716 | break; |
| 2717 | case com.android.internal.R.styleable.View_paddingEnd: |
| 2718 | endPadding = a.getDimensionPixelSize(attr, -1); |
| 2719 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | case com.android.internal.R.styleable.View_scrollX: |
| 2721 | x = a.getDimensionPixelOffset(attr, 0); |
| 2722 | break; |
| 2723 | case com.android.internal.R.styleable.View_scrollY: |
| 2724 | y = a.getDimensionPixelOffset(attr, 0); |
| 2725 | break; |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 2726 | case com.android.internal.R.styleable.View_alpha: |
| 2727 | setAlpha(a.getFloat(attr, 1f)); |
| 2728 | break; |
| 2729 | case com.android.internal.R.styleable.View_transformPivotX: |
| 2730 | setPivotX(a.getDimensionPixelOffset(attr, 0)); |
| 2731 | break; |
| 2732 | case com.android.internal.R.styleable.View_transformPivotY: |
| 2733 | setPivotY(a.getDimensionPixelOffset(attr, 0)); |
| 2734 | break; |
| 2735 | case com.android.internal.R.styleable.View_translationX: |
| 2736 | tx = a.getDimensionPixelOffset(attr, 0); |
| 2737 | transformSet = true; |
| 2738 | break; |
| 2739 | case com.android.internal.R.styleable.View_translationY: |
| 2740 | ty = a.getDimensionPixelOffset(attr, 0); |
| 2741 | transformSet = true; |
| 2742 | break; |
| 2743 | case com.android.internal.R.styleable.View_rotation: |
| 2744 | rotation = a.getFloat(attr, 0); |
| 2745 | transformSet = true; |
| 2746 | break; |
| 2747 | case com.android.internal.R.styleable.View_rotationX: |
| 2748 | rotationX = a.getFloat(attr, 0); |
| 2749 | transformSet = true; |
| 2750 | break; |
| 2751 | case com.android.internal.R.styleable.View_rotationY: |
| 2752 | rotationY = a.getFloat(attr, 0); |
| 2753 | transformSet = true; |
| 2754 | break; |
| 2755 | case com.android.internal.R.styleable.View_scaleX: |
| 2756 | sx = a.getFloat(attr, 1f); |
| 2757 | transformSet = true; |
| 2758 | break; |
| 2759 | case com.android.internal.R.styleable.View_scaleY: |
| 2760 | sy = a.getFloat(attr, 1f); |
| 2761 | transformSet = true; |
| 2762 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | case com.android.internal.R.styleable.View_id: |
| 2764 | mID = a.getResourceId(attr, NO_ID); |
| 2765 | break; |
| 2766 | case com.android.internal.R.styleable.View_tag: |
| 2767 | mTag = a.getText(attr); |
| 2768 | break; |
| 2769 | case com.android.internal.R.styleable.View_fitsSystemWindows: |
| 2770 | if (a.getBoolean(attr, false)) { |
| 2771 | viewFlagValues |= FITS_SYSTEM_WINDOWS; |
| 2772 | viewFlagMasks |= FITS_SYSTEM_WINDOWS; |
| 2773 | } |
| 2774 | break; |
| 2775 | case com.android.internal.R.styleable.View_focusable: |
| 2776 | if (a.getBoolean(attr, false)) { |
| 2777 | viewFlagValues |= FOCUSABLE; |
| 2778 | viewFlagMasks |= FOCUSABLE_MASK; |
| 2779 | } |
| 2780 | break; |
| 2781 | case com.android.internal.R.styleable.View_focusableInTouchMode: |
| 2782 | if (a.getBoolean(attr, false)) { |
| 2783 | viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE; |
| 2784 | viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK; |
| 2785 | } |
| 2786 | break; |
| 2787 | case com.android.internal.R.styleable.View_clickable: |
| 2788 | if (a.getBoolean(attr, false)) { |
| 2789 | viewFlagValues |= CLICKABLE; |
| 2790 | viewFlagMasks |= CLICKABLE; |
| 2791 | } |
| 2792 | break; |
| 2793 | case com.android.internal.R.styleable.View_longClickable: |
| 2794 | if (a.getBoolean(attr, false)) { |
| 2795 | viewFlagValues |= LONG_CLICKABLE; |
| 2796 | viewFlagMasks |= LONG_CLICKABLE; |
| 2797 | } |
| 2798 | break; |
| 2799 | case com.android.internal.R.styleable.View_saveEnabled: |
| 2800 | if (!a.getBoolean(attr, true)) { |
| 2801 | viewFlagValues |= SAVE_DISABLED; |
| 2802 | viewFlagMasks |= SAVE_DISABLED_MASK; |
| 2803 | } |
| 2804 | break; |
| 2805 | case com.android.internal.R.styleable.View_duplicateParentState: |
| 2806 | if (a.getBoolean(attr, false)) { |
| 2807 | viewFlagValues |= DUPLICATE_PARENT_STATE; |
| 2808 | viewFlagMasks |= DUPLICATE_PARENT_STATE; |
| 2809 | } |
| 2810 | break; |
| 2811 | case com.android.internal.R.styleable.View_visibility: |
| 2812 | final int visibility = a.getInt(attr, 0); |
| 2813 | if (visibility != 0) { |
| 2814 | viewFlagValues |= VISIBILITY_FLAGS[visibility]; |
| 2815 | viewFlagMasks |= VISIBILITY_MASK; |
| 2816 | } |
| 2817 | break; |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2818 | case com.android.internal.R.styleable.View_layoutDirection: |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 2819 | // Clear any HORIZONTAL_DIRECTION flag already set |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2820 | viewFlagValues &= ~LAYOUT_DIRECTION_MASK; |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 2821 | // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2822 | final int layoutDirection = a.getInt(attr, -1); |
| 2823 | if (layoutDirection != -1) { |
| 2824 | viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection]; |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 2825 | } else { |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2826 | // Set to default (LAYOUT_DIRECTION_INHERIT) |
| 2827 | viewFlagValues |= LAYOUT_DIRECTION_DEFAULT; |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 2828 | } |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 2829 | viewFlagMasks |= LAYOUT_DIRECTION_MASK; |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 2830 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | case com.android.internal.R.styleable.View_drawingCacheQuality: |
| 2832 | final int cacheQuality = a.getInt(attr, 0); |
| 2833 | if (cacheQuality != 0) { |
| 2834 | viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality]; |
| 2835 | viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK; |
| 2836 | } |
| 2837 | break; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2838 | case com.android.internal.R.styleable.View_contentDescription: |
| 2839 | mContentDescription = a.getString(attr); |
| 2840 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | case com.android.internal.R.styleable.View_soundEffectsEnabled: |
| 2842 | if (!a.getBoolean(attr, true)) { |
| 2843 | viewFlagValues &= ~SOUND_EFFECTS_ENABLED; |
| 2844 | viewFlagMasks |= SOUND_EFFECTS_ENABLED; |
| 2845 | } |
Karl Rosaen | 61ab270 | 2009-06-23 11:10:25 -0700 | [diff] [blame] | 2846 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2847 | case com.android.internal.R.styleable.View_hapticFeedbackEnabled: |
| 2848 | if (!a.getBoolean(attr, true)) { |
| 2849 | viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED; |
| 2850 | viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED; |
| 2851 | } |
Karl Rosaen | 61ab270 | 2009-06-23 11:10:25 -0700 | [diff] [blame] | 2852 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2853 | case R.styleable.View_scrollbars: |
| 2854 | final int scrollbars = a.getInt(attr, SCROLLBARS_NONE); |
| 2855 | if (scrollbars != SCROLLBARS_NONE) { |
| 2856 | viewFlagValues |= scrollbars; |
| 2857 | viewFlagMasks |= SCROLLBARS_MASK; |
| 2858 | initializeScrollbars(a); |
| 2859 | } |
| 2860 | break; |
| 2861 | case R.styleable.View_fadingEdge: |
| 2862 | final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE); |
| 2863 | if (fadingEdge != FADING_EDGE_NONE) { |
| 2864 | viewFlagValues |= fadingEdge; |
| 2865 | viewFlagMasks |= FADING_EDGE_MASK; |
| 2866 | initializeFadingEdge(a); |
| 2867 | } |
| 2868 | break; |
| 2869 | case R.styleable.View_scrollbarStyle: |
| 2870 | scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY); |
| 2871 | if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) { |
| 2872 | viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK; |
| 2873 | viewFlagMasks |= SCROLLBARS_STYLE_MASK; |
| 2874 | } |
| 2875 | break; |
| 2876 | case R.styleable.View_isScrollContainer: |
| 2877 | setScrollContainer = true; |
| 2878 | if (a.getBoolean(attr, false)) { |
| 2879 | setScrollContainer(true); |
| 2880 | } |
| 2881 | break; |
| 2882 | case com.android.internal.R.styleable.View_keepScreenOn: |
| 2883 | if (a.getBoolean(attr, false)) { |
| 2884 | viewFlagValues |= KEEP_SCREEN_ON; |
| 2885 | viewFlagMasks |= KEEP_SCREEN_ON; |
| 2886 | } |
| 2887 | break; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2888 | case R.styleable.View_filterTouchesWhenObscured: |
| 2889 | if (a.getBoolean(attr, false)) { |
| 2890 | viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED; |
| 2891 | viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED; |
| 2892 | } |
| 2893 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2894 | case R.styleable.View_nextFocusLeft: |
| 2895 | mNextFocusLeftId = a.getResourceId(attr, View.NO_ID); |
| 2896 | break; |
| 2897 | case R.styleable.View_nextFocusRight: |
| 2898 | mNextFocusRightId = a.getResourceId(attr, View.NO_ID); |
| 2899 | break; |
| 2900 | case R.styleable.View_nextFocusUp: |
| 2901 | mNextFocusUpId = a.getResourceId(attr, View.NO_ID); |
| 2902 | break; |
| 2903 | case R.styleable.View_nextFocusDown: |
| 2904 | mNextFocusDownId = a.getResourceId(attr, View.NO_ID); |
| 2905 | break; |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2906 | case R.styleable.View_nextFocusForward: |
| 2907 | mNextFocusForwardId = a.getResourceId(attr, View.NO_ID); |
| 2908 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2909 | case R.styleable.View_minWidth: |
| 2910 | mMinWidth = a.getDimensionPixelSize(attr, 0); |
| 2911 | break; |
| 2912 | case R.styleable.View_minHeight: |
| 2913 | mMinHeight = a.getDimensionPixelSize(attr, 0); |
| 2914 | break; |
Romain Guy | 9a81736 | 2009-05-01 10:57:14 -0700 | [diff] [blame] | 2915 | case R.styleable.View_onClick: |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2916 | if (context.isRestricted()) { |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 2917 | throw new IllegalStateException("The android:onClick attribute cannot " |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2918 | + "be used within a restricted context"); |
| 2919 | } |
| 2920 | |
Romain Guy | 9a81736 | 2009-05-01 10:57:14 -0700 | [diff] [blame] | 2921 | final String handlerName = a.getString(attr); |
| 2922 | if (handlerName != null) { |
| 2923 | setOnClickListener(new OnClickListener() { |
| 2924 | private Method mHandler; |
| 2925 | |
| 2926 | public void onClick(View v) { |
| 2927 | if (mHandler == null) { |
| 2928 | try { |
| 2929 | mHandler = getContext().getClass().getMethod(handlerName, |
| 2930 | View.class); |
| 2931 | } catch (NoSuchMethodException e) { |
Joe Onorato | 42e14d7 | 2010-03-11 14:51:17 -0800 | [diff] [blame] | 2932 | int id = getId(); |
| 2933 | String idText = id == NO_ID ? "" : " with id '" |
| 2934 | + getContext().getResources().getResourceEntryName( |
| 2935 | id) + "'"; |
Romain Guy | 9a81736 | 2009-05-01 10:57:14 -0700 | [diff] [blame] | 2936 | throw new IllegalStateException("Could not find a method " + |
Joe Onorato | 42e14d7 | 2010-03-11 14:51:17 -0800 | [diff] [blame] | 2937 | handlerName + "(View) in the activity " |
| 2938 | + getContext().getClass() + " for onClick handler" |
| 2939 | + " on view " + View.this.getClass() + idText, e); |
Romain Guy | 9a81736 | 2009-05-01 10:57:14 -0700 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | try { |
| 2944 | mHandler.invoke(getContext(), View.this); |
| 2945 | } catch (IllegalAccessException e) { |
| 2946 | throw new IllegalStateException("Could not execute non " |
| 2947 | + "public method of the activity", e); |
| 2948 | } catch (InvocationTargetException e) { |
| 2949 | throw new IllegalStateException("Could not execute " |
| 2950 | + "method of the activity", e); |
| 2951 | } |
| 2952 | } |
| 2953 | }); |
| 2954 | } |
| 2955 | break; |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 2956 | case R.styleable.View_overScrollMode: |
| 2957 | overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS); |
| 2958 | break; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 2959 | case R.styleable.View_verticalScrollbarPosition: |
| 2960 | mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT); |
| 2961 | break; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 2962 | case R.styleable.View_layerType: |
| 2963 | setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null); |
| 2964 | break; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 2965 | case R.styleable.View_textDirection: |
| 2966 | mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION); |
| 2967 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 2971 | setOverScrollMode(overScrollMode); |
| 2972 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2973 | if (background != null) { |
| 2974 | setBackgroundDrawable(background); |
| 2975 | } |
| 2976 | |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 2977 | mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0); |
| 2978 | |
Fabrice Di Meglio | f9e3650 | 2011-06-21 18:41:48 -0700 | [diff] [blame] | 2979 | // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved |
| 2980 | // layout direction). Those cached values will be used later during padding resolution. |
| 2981 | mUserPaddingStart = startPadding; |
| 2982 | mUserPaddingEnd = endPadding; |
| 2983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | if (padding >= 0) { |
| 2985 | leftPadding = padding; |
| 2986 | topPadding = padding; |
| 2987 | rightPadding = padding; |
| 2988 | bottomPadding = padding; |
| 2989 | } |
| 2990 | |
| 2991 | // If the user specified the padding (either with android:padding or |
| 2992 | // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise |
| 2993 | // use the default padding or the padding from the background drawable |
| 2994 | // (stored at this point in mPadding*) |
| 2995 | setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft, |
| 2996 | topPadding >= 0 ? topPadding : mPaddingTop, |
| 2997 | rightPadding >= 0 ? rightPadding : mPaddingRight, |
| 2998 | bottomPadding >= 0 ? bottomPadding : mPaddingBottom); |
| 2999 | |
| 3000 | if (viewFlagMasks != 0) { |
| 3001 | setFlags(viewFlagValues, viewFlagMasks); |
| 3002 | } |
| 3003 | |
| 3004 | // Needs to be called after mViewFlags is set |
| 3005 | if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) { |
| 3006 | recomputePadding(); |
| 3007 | } |
| 3008 | |
| 3009 | if (x != 0 || y != 0) { |
| 3010 | scrollTo(x, y); |
| 3011 | } |
| 3012 | |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 3013 | if (transformSet) { |
| 3014 | setTranslationX(tx); |
| 3015 | setTranslationY(ty); |
| 3016 | setRotation(rotation); |
| 3017 | setRotationX(rotationX); |
| 3018 | setRotationY(rotationY); |
| 3019 | setScaleX(sx); |
| 3020 | setScaleY(sy); |
| 3021 | } |
| 3022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3023 | if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) { |
| 3024 | setScrollContainer(true); |
| 3025 | } |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 3026 | |
| 3027 | computeOpaqueFlags(); |
| 3028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3029 | a.recycle(); |
| 3030 | } |
| 3031 | |
| 3032 | /** |
| 3033 | * Non-public constructor for use in testing |
| 3034 | */ |
| 3035 | View() { |
| 3036 | } |
| 3037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3038 | /** |
| 3039 | * <p> |
| 3040 | * Initializes the fading edges from a given set of styled attributes. This |
| 3041 | * method should be called by subclasses that need fading edges and when an |
| 3042 | * instance of these subclasses is created programmatically rather than |
| 3043 | * being inflated from XML. This method is automatically called when the XML |
| 3044 | * is inflated. |
| 3045 | * </p> |
| 3046 | * |
| 3047 | * @param a the styled attributes set to initialize the fading edges from |
| 3048 | */ |
| 3049 | protected void initializeFadingEdge(TypedArray a) { |
| 3050 | initScrollCache(); |
| 3051 | |
| 3052 | mScrollCache.fadingEdgeLength = a.getDimensionPixelSize( |
| 3053 | R.styleable.View_fadingEdgeLength, |
| 3054 | ViewConfiguration.get(mContext).getScaledFadingEdgeLength()); |
| 3055 | } |
| 3056 | |
| 3057 | /** |
| 3058 | * Returns the size of the vertical faded edges used to indicate that more |
| 3059 | * content in this view is visible. |
| 3060 | * |
| 3061 | * @return The size in pixels of the vertical faded edge or 0 if vertical |
| 3062 | * faded edges are not enabled for this view. |
| 3063 | * @attr ref android.R.styleable#View_fadingEdgeLength |
| 3064 | */ |
| 3065 | public int getVerticalFadingEdgeLength() { |
| 3066 | if (isVerticalFadingEdgeEnabled()) { |
| 3067 | ScrollabilityCache cache = mScrollCache; |
| 3068 | if (cache != null) { |
| 3069 | return cache.fadingEdgeLength; |
| 3070 | } |
| 3071 | } |
| 3072 | return 0; |
| 3073 | } |
| 3074 | |
| 3075 | /** |
| 3076 | * Set the size of the faded edge used to indicate that more content in this |
| 3077 | * view is available. Will not change whether the fading edge is enabled; use |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 3078 | * {@link #setVerticalFadingEdgeEnabled(boolean)} or |
| 3079 | * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge |
| 3080 | * for the vertical or horizontal fading edges. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3081 | * |
| 3082 | * @param length The size in pixels of the faded edge used to indicate that more |
| 3083 | * content in this view is visible. |
| 3084 | */ |
| 3085 | public void setFadingEdgeLength(int length) { |
| 3086 | initScrollCache(); |
| 3087 | mScrollCache.fadingEdgeLength = length; |
| 3088 | } |
| 3089 | |
| 3090 | /** |
| 3091 | * Returns the size of the horizontal faded edges used to indicate that more |
| 3092 | * content in this view is visible. |
| 3093 | * |
| 3094 | * @return The size in pixels of the horizontal faded edge or 0 if horizontal |
| 3095 | * faded edges are not enabled for this view. |
| 3096 | * @attr ref android.R.styleable#View_fadingEdgeLength |
| 3097 | */ |
| 3098 | public int getHorizontalFadingEdgeLength() { |
| 3099 | if (isHorizontalFadingEdgeEnabled()) { |
| 3100 | ScrollabilityCache cache = mScrollCache; |
| 3101 | if (cache != null) { |
| 3102 | return cache.fadingEdgeLength; |
| 3103 | } |
| 3104 | } |
| 3105 | return 0; |
| 3106 | } |
| 3107 | |
| 3108 | /** |
| 3109 | * Returns the width of the vertical scrollbar. |
| 3110 | * |
| 3111 | * @return The width in pixels of the vertical scrollbar or 0 if there |
| 3112 | * is no vertical scrollbar. |
| 3113 | */ |
| 3114 | public int getVerticalScrollbarWidth() { |
| 3115 | ScrollabilityCache cache = mScrollCache; |
| 3116 | if (cache != null) { |
| 3117 | ScrollBarDrawable scrollBar = cache.scrollBar; |
| 3118 | if (scrollBar != null) { |
| 3119 | int size = scrollBar.getSize(true); |
| 3120 | if (size <= 0) { |
| 3121 | size = cache.scrollBarSize; |
| 3122 | } |
| 3123 | return size; |
| 3124 | } |
| 3125 | return 0; |
| 3126 | } |
| 3127 | return 0; |
| 3128 | } |
| 3129 | |
| 3130 | /** |
| 3131 | * Returns the height of the horizontal scrollbar. |
| 3132 | * |
| 3133 | * @return The height in pixels of the horizontal scrollbar or 0 if |
| 3134 | * there is no horizontal scrollbar. |
| 3135 | */ |
| 3136 | protected int getHorizontalScrollbarHeight() { |
| 3137 | ScrollabilityCache cache = mScrollCache; |
| 3138 | if (cache != null) { |
| 3139 | ScrollBarDrawable scrollBar = cache.scrollBar; |
| 3140 | if (scrollBar != null) { |
| 3141 | int size = scrollBar.getSize(false); |
| 3142 | if (size <= 0) { |
| 3143 | size = cache.scrollBarSize; |
| 3144 | } |
| 3145 | return size; |
| 3146 | } |
| 3147 | return 0; |
| 3148 | } |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | /** |
| 3153 | * <p> |
| 3154 | * Initializes the scrollbars from a given set of styled attributes. This |
| 3155 | * method should be called by subclasses that need scrollbars and when an |
| 3156 | * instance of these subclasses is created programmatically rather than |
| 3157 | * being inflated from XML. This method is automatically called when the XML |
| 3158 | * is inflated. |
| 3159 | * </p> |
| 3160 | * |
| 3161 | * @param a the styled attributes set to initialize the scrollbars from |
| 3162 | */ |
| 3163 | protected void initializeScrollbars(TypedArray a) { |
| 3164 | initScrollCache(); |
| 3165 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3166 | final ScrollabilityCache scrollabilityCache = mScrollCache; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3167 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 3168 | if (scrollabilityCache.scrollBar == null) { |
| 3169 | scrollabilityCache.scrollBar = new ScrollBarDrawable(); |
| 3170 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3171 | |
Romain Guy | 8bda248 | 2010-03-02 11:42:11 -0800 | [diff] [blame] | 3172 | final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3173 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 3174 | if (!fadeScrollbars) { |
| 3175 | scrollabilityCache.state = ScrollabilityCache.ON; |
| 3176 | } |
| 3177 | scrollabilityCache.fadeScrollBars = fadeScrollbars; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3178 | |
| 3179 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 3180 | scrollabilityCache.scrollBarFadeDuration = a.getInt( |
| 3181 | R.styleable.View_scrollbarFadeDuration, ViewConfiguration |
| 3182 | .getScrollBarFadeDuration()); |
| 3183 | scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt( |
| 3184 | R.styleable.View_scrollbarDefaultDelayBeforeFade, |
| 3185 | ViewConfiguration.getScrollDefaultDelay()); |
| 3186 | |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3187 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3188 | scrollabilityCache.scrollBarSize = a.getDimensionPixelSize( |
| 3189 | com.android.internal.R.styleable.View_scrollbarSize, |
| 3190 | ViewConfiguration.get(mContext).getScaledScrollBarSize()); |
| 3191 | |
| 3192 | Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal); |
| 3193 | scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track); |
| 3194 | |
| 3195 | Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal); |
| 3196 | if (thumb != null) { |
| 3197 | scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb); |
| 3198 | } |
| 3199 | |
| 3200 | boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack, |
| 3201 | false); |
| 3202 | if (alwaysDraw) { |
| 3203 | scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true); |
| 3204 | } |
| 3205 | |
| 3206 | track = a.getDrawable(R.styleable.View_scrollbarTrackVertical); |
| 3207 | scrollabilityCache.scrollBar.setVerticalTrackDrawable(track); |
| 3208 | |
| 3209 | thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical); |
| 3210 | if (thumb != null) { |
| 3211 | scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb); |
| 3212 | } |
| 3213 | |
| 3214 | alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack, |
| 3215 | false); |
| 3216 | if (alwaysDraw) { |
| 3217 | scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true); |
| 3218 | } |
| 3219 | |
| 3220 | // Re-apply user/background padding so that scrollbar(s) get added |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 3221 | resolvePadding(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | /** |
| 3225 | * <p> |
| 3226 | * Initalizes the scrollability cache if necessary. |
| 3227 | * </p> |
| 3228 | */ |
| 3229 | private void initScrollCache() { |
| 3230 | if (mScrollCache == null) { |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 3231 | mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | /** |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 3236 | * Set the position of the vertical scroll bar. Should be one of |
| 3237 | * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or |
| 3238 | * {@link #SCROLLBAR_POSITION_RIGHT}. |
| 3239 | * |
| 3240 | * @param position Where the vertical scroll bar should be positioned. |
| 3241 | */ |
| 3242 | public void setVerticalScrollbarPosition(int position) { |
| 3243 | if (mVerticalScrollbarPosition != position) { |
| 3244 | mVerticalScrollbarPosition = position; |
| 3245 | computeOpaqueFlags(); |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 3246 | resolvePadding(); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | /** |
| 3251 | * @return The position where the vertical scroll bar will show, if applicable. |
| 3252 | * @see #setVerticalScrollbarPosition(int) |
| 3253 | */ |
| 3254 | public int getVerticalScrollbarPosition() { |
| 3255 | return mVerticalScrollbarPosition; |
| 3256 | } |
| 3257 | |
| 3258 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3259 | * Register a callback to be invoked when focus of this view changed. |
| 3260 | * |
| 3261 | * @param l The callback that will run. |
| 3262 | */ |
| 3263 | public void setOnFocusChangeListener(OnFocusChangeListener l) { |
| 3264 | mOnFocusChangeListener = l; |
| 3265 | } |
| 3266 | |
| 3267 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3268 | * Add a listener that will be called when the bounds of the view change due to |
| 3269 | * layout processing. |
| 3270 | * |
| 3271 | * @param listener The listener that will be called when layout bounds change. |
| 3272 | */ |
| 3273 | public void addOnLayoutChangeListener(OnLayoutChangeListener listener) { |
| 3274 | if (mOnLayoutChangeListeners == null) { |
| 3275 | mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>(); |
| 3276 | } |
| 3277 | mOnLayoutChangeListeners.add(listener); |
| 3278 | } |
| 3279 | |
| 3280 | /** |
| 3281 | * Remove a listener for layout changes. |
| 3282 | * |
| 3283 | * @param listener The listener for layout bounds change. |
| 3284 | */ |
| 3285 | public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) { |
| 3286 | if (mOnLayoutChangeListeners == null) { |
| 3287 | return; |
| 3288 | } |
| 3289 | mOnLayoutChangeListeners.remove(listener); |
| 3290 | } |
| 3291 | |
| 3292 | /** |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 3293 | * Add a listener for attach state changes. |
| 3294 | * |
| 3295 | * This listener will be called whenever this view is attached or detached |
| 3296 | * from a window. Remove the listener using |
| 3297 | * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}. |
| 3298 | * |
| 3299 | * @param listener Listener to attach |
| 3300 | * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener) |
| 3301 | */ |
| 3302 | public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) { |
| 3303 | if (mOnAttachStateChangeListeners == null) { |
| 3304 | mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>(); |
| 3305 | } |
| 3306 | mOnAttachStateChangeListeners.add(listener); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
| 3310 | * Remove a listener for attach state changes. The listener will receive no further |
| 3311 | * notification of window attach/detach events. |
| 3312 | * |
| 3313 | * @param listener Listener to remove |
| 3314 | * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener) |
| 3315 | */ |
| 3316 | public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) { |
| 3317 | if (mOnAttachStateChangeListeners == null) { |
| 3318 | return; |
| 3319 | } |
| 3320 | mOnAttachStateChangeListeners.remove(listener); |
| 3321 | } |
| 3322 | |
| 3323 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3324 | * Returns the focus-change callback registered for this view. |
| 3325 | * |
| 3326 | * @return The callback, or null if one is not registered. |
| 3327 | */ |
| 3328 | public OnFocusChangeListener getOnFocusChangeListener() { |
| 3329 | return mOnFocusChangeListener; |
| 3330 | } |
| 3331 | |
| 3332 | /** |
| 3333 | * Register a callback to be invoked when this view is clicked. If this view is not |
| 3334 | * clickable, it becomes clickable. |
| 3335 | * |
| 3336 | * @param l The callback that will run |
| 3337 | * |
| 3338 | * @see #setClickable(boolean) |
| 3339 | */ |
| 3340 | public void setOnClickListener(OnClickListener l) { |
| 3341 | if (!isClickable()) { |
| 3342 | setClickable(true); |
| 3343 | } |
| 3344 | mOnClickListener = l; |
| 3345 | } |
| 3346 | |
| 3347 | /** |
| 3348 | * Register a callback to be invoked when this view is clicked and held. If this view is not |
| 3349 | * long clickable, it becomes long clickable. |
| 3350 | * |
| 3351 | * @param l The callback that will run |
| 3352 | * |
| 3353 | * @see #setLongClickable(boolean) |
| 3354 | */ |
| 3355 | public void setOnLongClickListener(OnLongClickListener l) { |
| 3356 | if (!isLongClickable()) { |
| 3357 | setLongClickable(true); |
| 3358 | } |
| 3359 | mOnLongClickListener = l; |
| 3360 | } |
| 3361 | |
| 3362 | /** |
| 3363 | * Register a callback to be invoked when the context menu for this view is |
| 3364 | * being built. If this view is not long clickable, it becomes long clickable. |
| 3365 | * |
| 3366 | * @param l The callback that will run |
| 3367 | * |
| 3368 | */ |
| 3369 | public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) { |
| 3370 | if (!isLongClickable()) { |
| 3371 | setLongClickable(true); |
| 3372 | } |
| 3373 | mOnCreateContextMenuListener = l; |
| 3374 | } |
| 3375 | |
| 3376 | /** |
| 3377 | * Call this view's OnClickListener, if it is defined. |
| 3378 | * |
| 3379 | * @return True there was an assigned OnClickListener that was called, false |
| 3380 | * otherwise is returned. |
| 3381 | */ |
| 3382 | public boolean performClick() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3383 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 3384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3385 | if (mOnClickListener != null) { |
| 3386 | playSoundEffect(SoundEffectConstants.CLICK); |
| 3387 | mOnClickListener.onClick(this); |
| 3388 | return true; |
| 3389 | } |
| 3390 | |
| 3391 | return false; |
| 3392 | } |
| 3393 | |
| 3394 | /** |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 3395 | * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the |
| 3396 | * OnLongClickListener did not consume the event. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3397 | * |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 3398 | * @return True if one of the above receivers consumed the event, false otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3399 | */ |
| 3400 | public boolean performLongClick() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3401 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); |
| 3402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3403 | boolean handled = false; |
| 3404 | if (mOnLongClickListener != null) { |
| 3405 | handled = mOnLongClickListener.onLongClick(View.this); |
| 3406 | } |
| 3407 | if (!handled) { |
| 3408 | handled = showContextMenu(); |
| 3409 | } |
| 3410 | if (handled) { |
| 3411 | performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); |
| 3412 | } |
| 3413 | return handled; |
| 3414 | } |
| 3415 | |
| 3416 | /** |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3417 | * Performs button-related actions during a touch down event. |
| 3418 | * |
| 3419 | * @param event The event. |
| 3420 | * @return True if the down was consumed. |
| 3421 | * |
| 3422 | * @hide |
| 3423 | */ |
| 3424 | protected boolean performButtonActionOnTouchDown(MotionEvent event) { |
| 3425 | if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) { |
| 3426 | if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) { |
| 3427 | return true; |
| 3428 | } |
| 3429 | } |
| 3430 | return false; |
| 3431 | } |
| 3432 | |
| 3433 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | * Bring up the context menu for this view. |
| 3435 | * |
| 3436 | * @return Whether a context menu was displayed. |
| 3437 | */ |
| 3438 | public boolean showContextMenu() { |
| 3439 | return getParent().showContextMenuForChild(this); |
| 3440 | } |
| 3441 | |
| 3442 | /** |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3443 | * Bring up the context menu for this view, referring to the item under the specified point. |
| 3444 | * |
| 3445 | * @param x The referenced x coordinate. |
| 3446 | * @param y The referenced y coordinate. |
| 3447 | * @param metaState The keyboard modifiers that were pressed. |
| 3448 | * @return Whether a context menu was displayed. |
| 3449 | * |
| 3450 | * @hide |
| 3451 | */ |
| 3452 | public boolean showContextMenu(float x, float y, int metaState) { |
| 3453 | return showContextMenu(); |
| 3454 | } |
| 3455 | |
| 3456 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 3457 | * Start an action mode. |
| 3458 | * |
| 3459 | * @param callback Callback that will control the lifecycle of the action mode |
| 3460 | * @return The new action mode if it is started, null otherwise |
| 3461 | * |
| 3462 | * @see ActionMode |
| 3463 | */ |
| 3464 | public ActionMode startActionMode(ActionMode.Callback callback) { |
| 3465 | return getParent().startActionModeForChild(this, callback); |
| 3466 | } |
| 3467 | |
| 3468 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3469 | * Register a callback to be invoked when a key is pressed in this view. |
| 3470 | * @param l the key listener to attach to this view |
| 3471 | */ |
| 3472 | public void setOnKeyListener(OnKeyListener l) { |
| 3473 | mOnKeyListener = l; |
| 3474 | } |
| 3475 | |
| 3476 | /** |
| 3477 | * Register a callback to be invoked when a touch event is sent to this view. |
| 3478 | * @param l the touch listener to attach to this view |
| 3479 | */ |
| 3480 | public void setOnTouchListener(OnTouchListener l) { |
| 3481 | mOnTouchListener = l; |
| 3482 | } |
| 3483 | |
| 3484 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 3485 | * Register a callback to be invoked when a generic motion event is sent to this view. |
| 3486 | * @param l the generic motion listener to attach to this view |
| 3487 | */ |
| 3488 | public void setOnGenericMotionListener(OnGenericMotionListener l) { |
| 3489 | mOnGenericMotionListener = l; |
| 3490 | } |
| 3491 | |
| 3492 | /** |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 3493 | * Register a callback to be invoked when a hover event is sent to this view. |
| 3494 | * @param l the hover listener to attach to this view |
| 3495 | */ |
| 3496 | public void setOnHoverListener(OnHoverListener l) { |
| 3497 | mOnHoverListener = l; |
| 3498 | } |
| 3499 | |
| 3500 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3501 | * Register a drag event listener callback object for this View. The parameter is |
| 3502 | * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a |
| 3503 | * View, the system calls the |
| 3504 | * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method. |
| 3505 | * @param l An implementation of {@link android.view.View.OnDragListener}. |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 3506 | */ |
| 3507 | public void setOnDragListener(OnDragListener l) { |
| 3508 | mOnDragListener = l; |
| 3509 | } |
| 3510 | |
| 3511 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 3512 | * Give this view focus. This will cause |
| 3513 | * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3514 | * |
| 3515 | * Note: this does not check whether this {@link View} should get focus, it just |
| 3516 | * gives it focus no matter what. It should only be called internally by framework |
| 3517 | * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}. |
| 3518 | * |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 3519 | * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN}, |
| 3520 | * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3521 | * focus moved when requestFocus() is called. It may not always |
| 3522 | * apply, in which case use the default View.FOCUS_DOWN. |
| 3523 | * @param previouslyFocusedRect The rectangle of the view that had focus |
| 3524 | * prior in this View's coordinate system. |
| 3525 | */ |
| 3526 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 3527 | if (DBG) { |
| 3528 | System.out.println(this + " requestFocus()"); |
| 3529 | } |
| 3530 | |
| 3531 | if ((mPrivateFlags & FOCUSED) == 0) { |
| 3532 | mPrivateFlags |= FOCUSED; |
| 3533 | |
| 3534 | if (mParent != null) { |
| 3535 | mParent.requestChildFocus(this, this); |
| 3536 | } |
| 3537 | |
| 3538 | onFocusChanged(true, direction, previouslyFocusedRect); |
| 3539 | refreshDrawableState(); |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | /** |
| 3544 | * Request that a rectangle of this view be visible on the screen, |
| 3545 | * scrolling if necessary just enough. |
| 3546 | * |
| 3547 | * <p>A View should call this if it maintains some notion of which part |
| 3548 | * of its content is interesting. For example, a text editing view |
| 3549 | * should call this when its cursor moves. |
| 3550 | * |
| 3551 | * @param rectangle The rectangle. |
| 3552 | * @return Whether any parent scrolled. |
| 3553 | */ |
| 3554 | public boolean requestRectangleOnScreen(Rect rectangle) { |
| 3555 | return requestRectangleOnScreen(rectangle, false); |
| 3556 | } |
| 3557 | |
| 3558 | /** |
| 3559 | * Request that a rectangle of this view be visible on the screen, |
| 3560 | * scrolling if necessary just enough. |
| 3561 | * |
| 3562 | * <p>A View should call this if it maintains some notion of which part |
| 3563 | * of its content is interesting. For example, a text editing view |
| 3564 | * should call this when its cursor moves. |
| 3565 | * |
| 3566 | * <p>When <code>immediate</code> is set to true, scrolling will not be |
| 3567 | * animated. |
| 3568 | * |
| 3569 | * @param rectangle The rectangle. |
| 3570 | * @param immediate True to forbid animated scrolling, false otherwise |
| 3571 | * @return Whether any parent scrolled. |
| 3572 | */ |
| 3573 | public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) { |
| 3574 | View child = this; |
| 3575 | ViewParent parent = mParent; |
| 3576 | boolean scrolled = false; |
| 3577 | while (parent != null) { |
| 3578 | scrolled |= parent.requestChildRectangleOnScreen(child, |
| 3579 | rectangle, immediate); |
| 3580 | |
| 3581 | // offset rect so next call has the rectangle in the |
| 3582 | // coordinate system of its direct child. |
| 3583 | rectangle.offset(child.getLeft(), child.getTop()); |
| 3584 | rectangle.offset(-child.getScrollX(), -child.getScrollY()); |
| 3585 | |
| 3586 | if (!(parent instanceof View)) { |
| 3587 | break; |
| 3588 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | child = (View) parent; |
| 3591 | parent = child.getParent(); |
| 3592 | } |
| 3593 | return scrolled; |
| 3594 | } |
| 3595 | |
| 3596 | /** |
| 3597 | * Called when this view wants to give up focus. This will cause |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 3598 | * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3599 | */ |
| 3600 | public void clearFocus() { |
| 3601 | if (DBG) { |
| 3602 | System.out.println(this + " clearFocus()"); |
| 3603 | } |
| 3604 | |
| 3605 | if ((mPrivateFlags & FOCUSED) != 0) { |
| 3606 | mPrivateFlags &= ~FOCUSED; |
| 3607 | |
| 3608 | if (mParent != null) { |
| 3609 | mParent.clearChildFocus(this); |
| 3610 | } |
| 3611 | |
| 3612 | onFocusChanged(false, 0, null); |
| 3613 | refreshDrawableState(); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | /** |
| 3618 | * Called to clear the focus of a view that is about to be removed. |
| 3619 | * Doesn't call clearChildFocus, which prevents this view from taking |
| 3620 | * focus again before it has been removed from the parent |
| 3621 | */ |
| 3622 | void clearFocusForRemoval() { |
| 3623 | if ((mPrivateFlags & FOCUSED) != 0) { |
| 3624 | mPrivateFlags &= ~FOCUSED; |
| 3625 | |
| 3626 | onFocusChanged(false, 0, null); |
| 3627 | refreshDrawableState(); |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | /** |
| 3632 | * Called internally by the view system when a new view is getting focus. |
| 3633 | * This is what clears the old focus. |
| 3634 | */ |
| 3635 | void unFocus() { |
| 3636 | if (DBG) { |
| 3637 | System.out.println(this + " unFocus()"); |
| 3638 | } |
| 3639 | |
| 3640 | if ((mPrivateFlags & FOCUSED) != 0) { |
| 3641 | mPrivateFlags &= ~FOCUSED; |
| 3642 | |
| 3643 | onFocusChanged(false, 0, null); |
| 3644 | refreshDrawableState(); |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | /** |
| 3649 | * Returns true if this view has focus iteself, or is the ancestor of the |
| 3650 | * view that has focus. |
| 3651 | * |
| 3652 | * @return True if this view has or contains focus, false otherwise. |
| 3653 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 3654 | @ViewDebug.ExportedProperty(category = "focus") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3655 | public boolean hasFocus() { |
| 3656 | return (mPrivateFlags & FOCUSED) != 0; |
| 3657 | } |
| 3658 | |
| 3659 | /** |
| 3660 | * Returns true if this view is focusable or if it contains a reachable View |
| 3661 | * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()" |
| 3662 | * is a View whose parents do not block descendants focus. |
| 3663 | * |
| 3664 | * Only {@link #VISIBLE} views are considered focusable. |
| 3665 | * |
| 3666 | * @return True if the view is focusable or if the view contains a focusable |
| 3667 | * View, false otherwise. |
| 3668 | * |
| 3669 | * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS |
| 3670 | */ |
| 3671 | public boolean hasFocusable() { |
| 3672 | return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable(); |
| 3673 | } |
| 3674 | |
| 3675 | /** |
| 3676 | * Called by the view system when the focus state of this view changes. |
| 3677 | * When the focus change event is caused by directional navigation, direction |
| 3678 | * and previouslyFocusedRect provide insight into where the focus is coming from. |
| 3679 | * When overriding, be sure to call up through to the super class so that |
| 3680 | * the standard focus handling will occur. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3681 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3682 | * @param gainFocus True if the View has focus; false otherwise. |
| 3683 | * @param direction The direction focus has moved when requestFocus() |
| 3684 | * is called to give this view focus. Values are |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3685 | * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT}, |
| 3686 | * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}. |
| 3687 | * It may not always apply, in which case use the default. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | * @param previouslyFocusedRect The rectangle, in this view's coordinate |
| 3689 | * system, of the previously focused view. If applicable, this will be |
| 3690 | * passed in as finer grained information about where the focus is coming |
| 3691 | * from (in addition to direction). Will be <code>null</code> otherwise. |
| 3692 | */ |
| 3693 | protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3694 | if (gainFocus) { |
| 3695 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED); |
| 3696 | } |
| 3697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3698 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 3699 | if (!gainFocus) { |
| 3700 | if (isPressed()) { |
| 3701 | setPressed(false); |
| 3702 | } |
| 3703 | if (imm != null && mAttachInfo != null |
| 3704 | && mAttachInfo.mHasWindowFocus) { |
| 3705 | imm.focusOut(this); |
| 3706 | } |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 3707 | onFocusLost(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3708 | } else if (imm != null && mAttachInfo != null |
| 3709 | && mAttachInfo.mHasWindowFocus) { |
| 3710 | imm.focusIn(this); |
| 3711 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3712 | |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 3713 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | if (mOnFocusChangeListener != null) { |
| 3715 | mOnFocusChangeListener.onFocusChange(this, gainFocus); |
| 3716 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 3717 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 3718 | if (mAttachInfo != null) { |
| 3719 | mAttachInfo.mKeyDispatchState.reset(this); |
| 3720 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | /** |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3724 | * Sends an accessibility event of the given type. If accessiiblity is |
| 3725 | * not enabled this method has no effect. The default implementation calls |
| 3726 | * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first |
| 3727 | * to populate information about the event source (this View), then calls |
| 3728 | * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to |
| 3729 | * populate the text content of the event source including its descendants, |
| 3730 | * and last calls |
| 3731 | * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)} |
| 3732 | * on its parent to resuest sending of the event to interested parties. |
| 3733 | * |
| 3734 | * @param eventType The type of the event to send. |
| 3735 | * |
| 3736 | * @see #onInitializeAccessibilityEvent(AccessibilityEvent) |
| 3737 | * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent) |
| 3738 | * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent) |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3739 | */ |
| 3740 | public void sendAccessibilityEvent(int eventType) { |
| 3741 | if (AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 3742 | sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType)); |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | /** |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3747 | * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but |
| 3748 | * takes as an argument an empty {@link AccessibilityEvent} and does not |
| 3749 | * perfrom a check whether accessibility is enabled. |
| 3750 | * |
| 3751 | * @param event The event to send. |
| 3752 | * |
| 3753 | * @see #sendAccessibilityEvent(int) |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3754 | */ |
| 3755 | public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { |
Svetoslav Ganov | 9cd1eca | 2011-01-13 14:24:02 -0800 | [diff] [blame] | 3756 | if (!isShown()) { |
| 3757 | return; |
| 3758 | } |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3759 | onInitializeAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3760 | dispatchPopulateAccessibilityEvent(event); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3761 | // In the beginning we called #isShown(), so we know that getParent() is not null. |
| 3762 | getParent().requestSendAccessibilityEvent(this, event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | /** |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3766 | * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then |
| 3767 | * to its children for adding their text content to the event. Note that the |
| 3768 | * event text is populated in a separate dispatch path since we add to the |
| 3769 | * event not only the text of the source but also the text of all its descendants. |
| 3770 | * </p> |
| 3771 | * A typical implementation will call |
| 3772 | * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view |
| 3773 | * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} |
| 3774 | * on each child. Override this method if custom population of the event text |
| 3775 | * content is required. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3776 | * |
| 3777 | * @param event The event. |
| 3778 | * |
| 3779 | * @return True if the event population was completed. |
| 3780 | */ |
| 3781 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3782 | onPopulateAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3783 | return false; |
| 3784 | } |
| 3785 | |
| 3786 | /** |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3787 | * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3788 | * giving a chance to this View to populate the accessibility event with its |
| 3789 | * text content. While the implementation is free to modify other event |
| 3790 | * attributes this should be performed in |
| 3791 | * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}. |
| 3792 | * <p> |
| 3793 | * Example: Adding formatted date string to an accessibility event in addition |
| 3794 | * to the text added by the super implementation. |
| 3795 | * </p><p><pre><code> |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3796 | * public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 3797 | * super.onPopulateAccessibilityEvent(event); |
| 3798 | * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY; |
| 3799 | * String selectedDateUtterance = DateUtils.formatDateTime(mContext, |
| 3800 | * mCurrentDate.getTimeInMillis(), flags); |
| 3801 | * event.getText().add(selectedDateUtterance); |
| 3802 | * } |
| 3803 | * </code></pre></p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3804 | * |
| 3805 | * @param event The accessibility event which to populate. |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3806 | * |
| 3807 | * @see #sendAccessibilityEvent(int) |
| 3808 | * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent) |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3809 | */ |
| 3810 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /** |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3814 | * Initializes an {@link AccessibilityEvent} with information about the |
| 3815 | * the type of the event and this View which is the event source. In other |
| 3816 | * words, the source of an accessibility event is the view whose state |
| 3817 | * change triggered firing the event. |
| 3818 | * <p> |
| 3819 | * Example: Setting the password property of an event in addition |
| 3820 | * to properties set by the super implementation. |
| 3821 | * </p><p><pre><code> |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3822 | * public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 3823 | * super.onInitializeAccessibilityEvent(event); |
| 3824 | * event.setPassword(true); |
| 3825 | * } |
| 3826 | * </code></pre></p> |
| 3827 | * @param event The event to initialeze. |
| 3828 | * |
| 3829 | * @see #sendAccessibilityEvent(int) |
| 3830 | * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent) |
| 3831 | */ |
| 3832 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3833 | event.setSource(this); |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3834 | event.setClassName(getClass().getName()); |
| 3835 | event.setPackageName(getContext().getPackageName()); |
| 3836 | event.setEnabled(isEnabled()); |
| 3837 | event.setContentDescription(mContentDescription); |
| 3838 | |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 3839 | final int eventType = event.getEventType(); |
| 3840 | switch (eventType) { |
| 3841 | case AccessibilityEvent.TYPE_VIEW_FOCUSED: { |
| 3842 | if (mAttachInfo != null) { |
| 3843 | ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList; |
| 3844 | getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, |
| 3845 | FOCUSABLES_ALL); |
| 3846 | event.setItemCount(focusablesTempList.size()); |
| 3847 | event.setCurrentItemIndex(focusablesTempList.indexOf(this)); |
| 3848 | focusablesTempList.clear(); |
| 3849 | } |
| 3850 | } break; |
| 3851 | case AccessibilityEvent.TYPE_VIEW_SCROLLED: { |
| 3852 | event.setScrollX(mScrollX); |
| 3853 | event.setScrollY(mScrollY); |
| 3854 | event.setItemCount(getHeight()); |
| 3855 | } break; |
Svetoslav Ganov | 3040132 | 2011-05-12 18:53:45 -0700 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3860 | * Returns an {@link AccessibilityNodeInfo} representing this view from the |
| 3861 | * point of view of an {@link android.accessibilityservice.AccessibilityService}. |
| 3862 | * This method is responsible for obtaining an accessibility node info from a |
| 3863 | * pool of reusable instances and calling |
| 3864 | * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to |
| 3865 | * initialize the former. |
| 3866 | * <p> |
| 3867 | * Note: The client is responsible for recycling the obtained instance by calling |
| 3868 | * {@link AccessibilityNodeInfo#recycle()} to minimize object creation. |
| 3869 | * </p> |
| 3870 | * @return A populated {@link AccessibilityNodeInfo}. |
| 3871 | * |
| 3872 | * @see AccessibilityNodeInfo |
| 3873 | */ |
| 3874 | public AccessibilityNodeInfo createAccessibilityNodeInfo() { |
| 3875 | AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this); |
| 3876 | onInitializeAccessibilityNodeInfo(info); |
| 3877 | return info; |
| 3878 | } |
| 3879 | |
| 3880 | /** |
| 3881 | * Initializes an {@link AccessibilityNodeInfo} with information about this view. |
| 3882 | * The base implementation sets: |
| 3883 | * <ul> |
| 3884 | * <li>{@link AccessibilityNodeInfo#setParent(View)},</li> |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 3885 | * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li> |
| 3886 | * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li> |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3887 | * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li> |
| 3888 | * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li> |
| 3889 | * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li> |
| 3890 | * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li> |
| 3891 | * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li> |
| 3892 | * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li> |
| 3893 | * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li> |
| 3894 | * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li> |
| 3895 | * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li> |
| 3896 | * </ul> |
| 3897 | * <p> |
| 3898 | * Subclasses should override this method, call the super implementation, |
| 3899 | * and set additional attributes. |
| 3900 | * </p> |
| 3901 | * @param info The instance to initialize. |
| 3902 | */ |
| 3903 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 3904 | Rect bounds = mAttachInfo.mTmpInvalRect; |
| 3905 | getDrawingRect(bounds); |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 3906 | info.setBoundsInParent(bounds); |
| 3907 | |
| 3908 | int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation; |
| 3909 | getLocationOnScreen(locationOnScreen); |
Alan Viverette | 326804f | 2011-07-22 16:20:41 -0700 | [diff] [blame] | 3910 | bounds.offsetTo(0, 0); |
Svetoslav Ganov | eeee4d2 | 2011-06-10 20:51:30 -0700 | [diff] [blame] | 3911 | bounds.offset(locationOnScreen[0], locationOnScreen[1]); |
| 3912 | info.setBoundsInScreen(bounds); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3913 | |
| 3914 | ViewParent parent = getParent(); |
| 3915 | if (parent instanceof View) { |
| 3916 | View parentView = (View) parent; |
| 3917 | info.setParent(parentView); |
| 3918 | } |
| 3919 | |
| 3920 | info.setPackageName(mContext.getPackageName()); |
| 3921 | info.setClassName(getClass().getName()); |
| 3922 | info.setContentDescription(getContentDescription()); |
| 3923 | |
| 3924 | info.setEnabled(isEnabled()); |
| 3925 | info.setClickable(isClickable()); |
| 3926 | info.setFocusable(isFocusable()); |
| 3927 | info.setFocused(isFocused()); |
| 3928 | info.setSelected(isSelected()); |
| 3929 | info.setLongClickable(isLongClickable()); |
| 3930 | |
| 3931 | // TODO: These make sense only if we are in an AdapterView but all |
| 3932 | // views can be selected. Maybe from accessiiblity perspective |
| 3933 | // we should report as selectable view in an AdapterView. |
| 3934 | info.addAction(AccessibilityNodeInfo.ACTION_SELECT); |
| 3935 | info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION); |
| 3936 | |
| 3937 | if (isFocusable()) { |
| 3938 | if (isFocused()) { |
| 3939 | info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS); |
| 3940 | } else { |
| 3941 | info.addAction(AccessibilityNodeInfo.ACTION_FOCUS); |
| 3942 | } |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | /** |
| 3947 | * Gets the unique identifier of this view on the screen for accessibility purposes. |
| 3948 | * If this {@link View} is not attached to any window, {@value #NO_ID} is returned. |
| 3949 | * |
| 3950 | * @return The view accessibility id. |
| 3951 | * |
| 3952 | * @hide |
| 3953 | */ |
| 3954 | public int getAccessibilityViewId() { |
| 3955 | if (mAccessibilityViewId == NO_ID) { |
| 3956 | mAccessibilityViewId = sNextAccessibilityViewId++; |
| 3957 | } |
| 3958 | return mAccessibilityViewId; |
| 3959 | } |
| 3960 | |
| 3961 | /** |
| 3962 | * Gets the unique identifier of the window in which this View reseides. |
| 3963 | * |
| 3964 | * @return The window accessibility id. |
| 3965 | * |
| 3966 | * @hide |
| 3967 | */ |
| 3968 | public int getAccessibilityWindowId() { |
| 3969 | return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID; |
| 3970 | } |
| 3971 | |
| 3972 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3973 | * Gets the {@link View} description. It briefly describes the view and is |
| 3974 | * primarily used for accessibility support. Set this property to enable |
| 3975 | * better accessibility support for your application. This is especially |
| 3976 | * true for views that do not have textual representation (For example, |
| 3977 | * ImageButton). |
| 3978 | * |
| 3979 | * @return The content descriptiopn. |
| 3980 | * |
| 3981 | * @attr ref android.R.styleable#View_contentDescription |
| 3982 | */ |
| 3983 | public CharSequence getContentDescription() { |
| 3984 | return mContentDescription; |
| 3985 | } |
| 3986 | |
| 3987 | /** |
| 3988 | * Sets the {@link View} description. It briefly describes the view and is |
| 3989 | * primarily used for accessibility support. Set this property to enable |
| 3990 | * better accessibility support for your application. This is especially |
| 3991 | * true for views that do not have textual representation (For example, |
| 3992 | * ImageButton). |
| 3993 | * |
| 3994 | * @param contentDescription The content description. |
| 3995 | * |
| 3996 | * @attr ref android.R.styleable#View_contentDescription |
| 3997 | */ |
| 3998 | public void setContentDescription(CharSequence contentDescription) { |
| 3999 | mContentDescription = contentDescription; |
| 4000 | } |
| 4001 | |
| 4002 | /** |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 4003 | * Invoked whenever this view loses focus, either by losing window focus or by losing |
| 4004 | * focus within its window. This method can be used to clear any state tied to the |
| 4005 | * focus. For instance, if a button is held pressed with the trackball and the window |
| 4006 | * loses focus, this method can be used to cancel the press. |
| 4007 | * |
| 4008 | * Subclasses of View overriding this method should always call super.onFocusLost(). |
| 4009 | * |
| 4010 | * @see #onFocusChanged(boolean, int, android.graphics.Rect) |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4011 | * @see #onWindowFocusChanged(boolean) |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 4012 | * |
| 4013 | * @hide pending API council approval |
| 4014 | */ |
| 4015 | protected void onFocusLost() { |
| 4016 | resetPressedState(); |
| 4017 | } |
| 4018 | |
| 4019 | private void resetPressedState() { |
| 4020 | if ((mViewFlags & ENABLED_MASK) == DISABLED) { |
| 4021 | return; |
| 4022 | } |
| 4023 | |
| 4024 | if (isPressed()) { |
| 4025 | setPressed(false); |
| 4026 | |
| 4027 | if (!mHasPerformedLongPress) { |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 4028 | removeLongPressCallback(); |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4034 | * Returns true if this view has focus |
| 4035 | * |
| 4036 | * @return True if this view has focus, false otherwise. |
| 4037 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4038 | @ViewDebug.ExportedProperty(category = "focus") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4039 | public boolean isFocused() { |
| 4040 | return (mPrivateFlags & FOCUSED) != 0; |
| 4041 | } |
| 4042 | |
| 4043 | /** |
| 4044 | * Find the view in the hierarchy rooted at this view that currently has |
| 4045 | * focus. |
| 4046 | * |
| 4047 | * @return The view that currently has focus, or null if no focused view can |
| 4048 | * be found. |
| 4049 | */ |
| 4050 | public View findFocus() { |
| 4051 | return (mPrivateFlags & FOCUSED) != 0 ? this : null; |
| 4052 | } |
| 4053 | |
| 4054 | /** |
| 4055 | * Change whether this view is one of the set of scrollable containers in |
| 4056 | * its window. This will be used to determine whether the window can |
| 4057 | * resize or must pan when a soft input area is open -- scrollable |
| 4058 | * containers allow the window to use resize mode since the container |
| 4059 | * will appropriately shrink. |
| 4060 | */ |
| 4061 | public void setScrollContainer(boolean isScrollContainer) { |
| 4062 | if (isScrollContainer) { |
| 4063 | if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) { |
| 4064 | mAttachInfo.mScrollContainers.add(this); |
| 4065 | mPrivateFlags |= SCROLL_CONTAINER_ADDED; |
| 4066 | } |
| 4067 | mPrivateFlags |= SCROLL_CONTAINER; |
| 4068 | } else { |
| 4069 | if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) { |
| 4070 | mAttachInfo.mScrollContainers.remove(this); |
| 4071 | } |
| 4072 | mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED); |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | * Returns the quality of the drawing cache. |
| 4078 | * |
| 4079 | * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO}, |
| 4080 | * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH} |
| 4081 | * |
| 4082 | * @see #setDrawingCacheQuality(int) |
| 4083 | * @see #setDrawingCacheEnabled(boolean) |
| 4084 | * @see #isDrawingCacheEnabled() |
| 4085 | * |
| 4086 | * @attr ref android.R.styleable#View_drawingCacheQuality |
| 4087 | */ |
| 4088 | public int getDrawingCacheQuality() { |
| 4089 | return mViewFlags & DRAWING_CACHE_QUALITY_MASK; |
| 4090 | } |
| 4091 | |
| 4092 | /** |
| 4093 | * Set the drawing cache quality of this view. This value is used only when the |
| 4094 | * drawing cache is enabled |
| 4095 | * |
| 4096 | * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO}, |
| 4097 | * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH} |
| 4098 | * |
| 4099 | * @see #getDrawingCacheQuality() |
| 4100 | * @see #setDrawingCacheEnabled(boolean) |
| 4101 | * @see #isDrawingCacheEnabled() |
| 4102 | * |
| 4103 | * @attr ref android.R.styleable#View_drawingCacheQuality |
| 4104 | */ |
| 4105 | public void setDrawingCacheQuality(int quality) { |
| 4106 | setFlags(quality, DRAWING_CACHE_QUALITY_MASK); |
| 4107 | } |
| 4108 | |
| 4109 | /** |
| 4110 | * Returns whether the screen should remain on, corresponding to the current |
| 4111 | * value of {@link #KEEP_SCREEN_ON}. |
| 4112 | * |
| 4113 | * @return Returns true if {@link #KEEP_SCREEN_ON} is set. |
| 4114 | * |
| 4115 | * @see #setKeepScreenOn(boolean) |
| 4116 | * |
| 4117 | * @attr ref android.R.styleable#View_keepScreenOn |
| 4118 | */ |
| 4119 | public boolean getKeepScreenOn() { |
| 4120 | return (mViewFlags & KEEP_SCREEN_ON) != 0; |
| 4121 | } |
| 4122 | |
| 4123 | /** |
| 4124 | * Controls whether the screen should remain on, modifying the |
| 4125 | * value of {@link #KEEP_SCREEN_ON}. |
| 4126 | * |
| 4127 | * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}. |
| 4128 | * |
| 4129 | * @see #getKeepScreenOn() |
| 4130 | * |
| 4131 | * @attr ref android.R.styleable#View_keepScreenOn |
| 4132 | */ |
| 4133 | public void setKeepScreenOn(boolean keepScreenOn) { |
| 4134 | setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON); |
| 4135 | } |
| 4136 | |
| 4137 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4138 | * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}. |
| 4139 | * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4140 | * |
| 4141 | * @attr ref android.R.styleable#View_nextFocusLeft |
| 4142 | */ |
| 4143 | public int getNextFocusLeftId() { |
| 4144 | return mNextFocusLeftId; |
| 4145 | } |
| 4146 | |
| 4147 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4148 | * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}. |
| 4149 | * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should |
| 4150 | * decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4151 | * |
| 4152 | * @attr ref android.R.styleable#View_nextFocusLeft |
| 4153 | */ |
| 4154 | public void setNextFocusLeftId(int nextFocusLeftId) { |
| 4155 | mNextFocusLeftId = nextFocusLeftId; |
| 4156 | } |
| 4157 | |
| 4158 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4159 | * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}. |
| 4160 | * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4161 | * |
| 4162 | * @attr ref android.R.styleable#View_nextFocusRight |
| 4163 | */ |
| 4164 | public int getNextFocusRightId() { |
| 4165 | return mNextFocusRightId; |
| 4166 | } |
| 4167 | |
| 4168 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4169 | * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}. |
| 4170 | * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should |
| 4171 | * decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4172 | * |
| 4173 | * @attr ref android.R.styleable#View_nextFocusRight |
| 4174 | */ |
| 4175 | public void setNextFocusRightId(int nextFocusRightId) { |
| 4176 | mNextFocusRightId = nextFocusRightId; |
| 4177 | } |
| 4178 | |
| 4179 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4180 | * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}. |
| 4181 | * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4182 | * |
| 4183 | * @attr ref android.R.styleable#View_nextFocusUp |
| 4184 | */ |
| 4185 | public int getNextFocusUpId() { |
| 4186 | return mNextFocusUpId; |
| 4187 | } |
| 4188 | |
| 4189 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4190 | * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}. |
| 4191 | * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should |
| 4192 | * decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4193 | * |
| 4194 | * @attr ref android.R.styleable#View_nextFocusUp |
| 4195 | */ |
| 4196 | public void setNextFocusUpId(int nextFocusUpId) { |
| 4197 | mNextFocusUpId = nextFocusUpId; |
| 4198 | } |
| 4199 | |
| 4200 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4201 | * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}. |
| 4202 | * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4203 | * |
| 4204 | * @attr ref android.R.styleable#View_nextFocusDown |
| 4205 | */ |
| 4206 | public int getNextFocusDownId() { |
| 4207 | return mNextFocusDownId; |
| 4208 | } |
| 4209 | |
| 4210 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4211 | * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}. |
| 4212 | * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should |
| 4213 | * decide automatically. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | * |
| 4215 | * @attr ref android.R.styleable#View_nextFocusDown |
| 4216 | */ |
| 4217 | public void setNextFocusDownId(int nextFocusDownId) { |
| 4218 | mNextFocusDownId = nextFocusDownId; |
| 4219 | } |
| 4220 | |
| 4221 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4222 | * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}. |
| 4223 | * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically. |
| 4224 | * |
| 4225 | * @attr ref android.R.styleable#View_nextFocusForward |
| 4226 | */ |
| 4227 | public int getNextFocusForwardId() { |
| 4228 | return mNextFocusForwardId; |
| 4229 | } |
| 4230 | |
| 4231 | /** |
| 4232 | * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}. |
| 4233 | * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should |
| 4234 | * decide automatically. |
| 4235 | * |
| 4236 | * @attr ref android.R.styleable#View_nextFocusForward |
| 4237 | */ |
| 4238 | public void setNextFocusForwardId(int nextFocusForwardId) { |
| 4239 | mNextFocusForwardId = nextFocusForwardId; |
| 4240 | } |
| 4241 | |
| 4242 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4243 | * Returns the visibility of this view and all of its ancestors |
| 4244 | * |
| 4245 | * @return True if this view and all of its ancestors are {@link #VISIBLE} |
| 4246 | */ |
| 4247 | public boolean isShown() { |
| 4248 | View current = this; |
| 4249 | //noinspection ConstantConditions |
| 4250 | do { |
| 4251 | if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 4252 | return false; |
| 4253 | } |
| 4254 | ViewParent parent = current.mParent; |
| 4255 | if (parent == null) { |
| 4256 | return false; // We are not attached to the view root |
| 4257 | } |
| 4258 | if (!(parent instanceof View)) { |
| 4259 | return true; |
| 4260 | } |
| 4261 | current = (View) parent; |
| 4262 | } while (current != null); |
| 4263 | |
| 4264 | return false; |
| 4265 | } |
| 4266 | |
| 4267 | /** |
| 4268 | * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag |
| 4269 | * is set |
| 4270 | * |
| 4271 | * @param insets Insets for system windows |
| 4272 | * |
| 4273 | * @return True if this view applied the insets, false otherwise |
| 4274 | */ |
| 4275 | protected boolean fitSystemWindows(Rect insets) { |
| 4276 | if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) { |
| 4277 | mPaddingLeft = insets.left; |
| 4278 | mPaddingTop = insets.top; |
| 4279 | mPaddingRight = insets.right; |
| 4280 | mPaddingBottom = insets.bottom; |
| 4281 | requestLayout(); |
| 4282 | return true; |
| 4283 | } |
| 4284 | return false; |
| 4285 | } |
| 4286 | |
| 4287 | /** |
Adam Powell | 0bd1d0a | 2011-07-22 19:35:06 -0700 | [diff] [blame] | 4288 | * Set whether or not this view should account for system screen decorations |
| 4289 | * such as the status bar and inset its content. This allows this view to be |
| 4290 | * positioned in absolute screen coordinates and remain visible to the user. |
| 4291 | * |
| 4292 | * <p>This should only be used by top-level window decor views. |
| 4293 | * |
| 4294 | * @param fitSystemWindows true to inset content for system screen decorations, false for |
| 4295 | * default behavior. |
| 4296 | * |
| 4297 | * @attr ref android.R.styleable#View_fitsSystemWindows |
| 4298 | */ |
| 4299 | public void setFitsSystemWindows(boolean fitSystemWindows) { |
| 4300 | setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS); |
| 4301 | } |
| 4302 | |
| 4303 | /** |
| 4304 | * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view |
| 4305 | * will account for system screen decorations such as the status bar and inset its |
| 4306 | * content. This allows the view to be positioned in absolute screen coordinates |
| 4307 | * and remain visible to the user. |
| 4308 | * |
| 4309 | * @return true if this view will adjust its content bounds for system screen decorations. |
| 4310 | * |
| 4311 | * @attr ref android.R.styleable#View_fitsSystemWindows |
| 4312 | */ |
| 4313 | public boolean fitsSystemWindows() { |
| 4314 | return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS; |
| 4315 | } |
| 4316 | |
| 4317 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4318 | * Returns the visibility status for this view. |
| 4319 | * |
| 4320 | * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. |
| 4321 | * @attr ref android.R.styleable#View_visibility |
| 4322 | */ |
| 4323 | @ViewDebug.ExportedProperty(mapping = { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 4324 | @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"), |
| 4325 | @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"), |
| 4326 | @ViewDebug.IntToString(from = GONE, to = "GONE") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4327 | }) |
| 4328 | public int getVisibility() { |
| 4329 | return mViewFlags & VISIBILITY_MASK; |
| 4330 | } |
| 4331 | |
| 4332 | /** |
| 4333 | * Set the enabled state of this view. |
| 4334 | * |
| 4335 | * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. |
| 4336 | * @attr ref android.R.styleable#View_visibility |
| 4337 | */ |
| 4338 | @RemotableViewMethod |
| 4339 | public void setVisibility(int visibility) { |
| 4340 | setFlags(visibility, VISIBILITY_MASK); |
| 4341 | if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false); |
| 4342 | } |
| 4343 | |
| 4344 | /** |
| 4345 | * Returns the enabled status for this view. The interpretation of the |
| 4346 | * enabled state varies by subclass. |
| 4347 | * |
| 4348 | * @return True if this view is enabled, false otherwise. |
| 4349 | */ |
| 4350 | @ViewDebug.ExportedProperty |
| 4351 | public boolean isEnabled() { |
| 4352 | return (mViewFlags & ENABLED_MASK) == ENABLED; |
| 4353 | } |
| 4354 | |
| 4355 | /** |
| 4356 | * Set the enabled state of this view. The interpretation of the enabled |
| 4357 | * state varies by subclass. |
| 4358 | * |
| 4359 | * @param enabled True if this view is enabled, false otherwise. |
| 4360 | */ |
Jeff Sharkey | 2b95c24 | 2010-02-08 17:40:30 -0800 | [diff] [blame] | 4361 | @RemotableViewMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4362 | public void setEnabled(boolean enabled) { |
Amith Yamasani | a2ef00b | 2009-07-30 16:14:34 -0700 | [diff] [blame] | 4363 | if (enabled == isEnabled()) return; |
| 4364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4365 | setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK); |
| 4366 | |
| 4367 | /* |
| 4368 | * The View most likely has to change its appearance, so refresh |
| 4369 | * the drawable state. |
| 4370 | */ |
| 4371 | refreshDrawableState(); |
| 4372 | |
| 4373 | // Invalidate too, since the default behavior for views is to be |
| 4374 | // be drawn at 50% alpha rather than to change the drawable. |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 4375 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | /** |
| 4379 | * Set whether this view can receive the focus. |
| 4380 | * |
| 4381 | * Setting this to false will also ensure that this view is not focusable |
| 4382 | * in touch mode. |
| 4383 | * |
| 4384 | * @param focusable If true, this view can receive the focus. |
| 4385 | * |
| 4386 | * @see #setFocusableInTouchMode(boolean) |
| 4387 | * @attr ref android.R.styleable#View_focusable |
| 4388 | */ |
| 4389 | public void setFocusable(boolean focusable) { |
| 4390 | if (!focusable) { |
| 4391 | setFlags(0, FOCUSABLE_IN_TOUCH_MODE); |
| 4392 | } |
| 4393 | setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK); |
| 4394 | } |
| 4395 | |
| 4396 | /** |
| 4397 | * Set whether this view can receive focus while in touch mode. |
| 4398 | * |
| 4399 | * Setting this to true will also ensure that this view is focusable. |
| 4400 | * |
| 4401 | * @param focusableInTouchMode If true, this view can receive the focus while |
| 4402 | * in touch mode. |
| 4403 | * |
| 4404 | * @see #setFocusable(boolean) |
| 4405 | * @attr ref android.R.styleable#View_focusableInTouchMode |
| 4406 | */ |
| 4407 | public void setFocusableInTouchMode(boolean focusableInTouchMode) { |
| 4408 | // Focusable in touch mode should always be set before the focusable flag |
| 4409 | // otherwise, setting the focusable flag will trigger a focusableViewAvailable() |
| 4410 | // which, in touch mode, will not successfully request focus on this view |
| 4411 | // because the focusable in touch mode flag is not set |
| 4412 | setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE); |
| 4413 | if (focusableInTouchMode) { |
| 4414 | setFlags(FOCUSABLE, FOCUSABLE_MASK); |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | /** |
| 4419 | * Set whether this view should have sound effects enabled for events such as |
| 4420 | * clicking and touching. |
| 4421 | * |
| 4422 | * <p>You may wish to disable sound effects for a view if you already play sounds, |
| 4423 | * for instance, a dial key that plays dtmf tones. |
| 4424 | * |
| 4425 | * @param soundEffectsEnabled whether sound effects are enabled for this view. |
| 4426 | * @see #isSoundEffectsEnabled() |
| 4427 | * @see #playSoundEffect(int) |
| 4428 | * @attr ref android.R.styleable#View_soundEffectsEnabled |
| 4429 | */ |
| 4430 | public void setSoundEffectsEnabled(boolean soundEffectsEnabled) { |
| 4431 | setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED); |
| 4432 | } |
| 4433 | |
| 4434 | /** |
| 4435 | * @return whether this view should have sound effects enabled for events such as |
| 4436 | * clicking and touching. |
| 4437 | * |
| 4438 | * @see #setSoundEffectsEnabled(boolean) |
| 4439 | * @see #playSoundEffect(int) |
| 4440 | * @attr ref android.R.styleable#View_soundEffectsEnabled |
| 4441 | */ |
| 4442 | @ViewDebug.ExportedProperty |
| 4443 | public boolean isSoundEffectsEnabled() { |
| 4444 | return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED); |
| 4445 | } |
| 4446 | |
| 4447 | /** |
| 4448 | * Set whether this view should have haptic feedback for events such as |
| 4449 | * long presses. |
| 4450 | * |
| 4451 | * <p>You may wish to disable haptic feedback if your view already controls |
| 4452 | * its own haptic feedback. |
| 4453 | * |
| 4454 | * @param hapticFeedbackEnabled whether haptic feedback enabled for this view. |
| 4455 | * @see #isHapticFeedbackEnabled() |
| 4456 | * @see #performHapticFeedback(int) |
| 4457 | * @attr ref android.R.styleable#View_hapticFeedbackEnabled |
| 4458 | */ |
| 4459 | public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) { |
| 4460 | setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED); |
| 4461 | } |
| 4462 | |
| 4463 | /** |
| 4464 | * @return whether this view should have haptic feedback enabled for events |
| 4465 | * long presses. |
| 4466 | * |
| 4467 | * @see #setHapticFeedbackEnabled(boolean) |
| 4468 | * @see #performHapticFeedback(int) |
| 4469 | * @attr ref android.R.styleable#View_hapticFeedbackEnabled |
| 4470 | */ |
| 4471 | @ViewDebug.ExportedProperty |
| 4472 | public boolean isHapticFeedbackEnabled() { |
| 4473 | return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED); |
| 4474 | } |
| 4475 | |
| 4476 | /** |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4477 | * Returns the layout direction for this view. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4478 | * |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4479 | * @return One of {@link #LAYOUT_DIRECTION_LTR}, |
| 4480 | * {@link #LAYOUT_DIRECTION_RTL}, |
| 4481 | * {@link #LAYOUT_DIRECTION_INHERIT} or |
| 4482 | * {@link #LAYOUT_DIRECTION_LOCALE}. |
| 4483 | * @attr ref android.R.styleable#View_layoutDirection |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4484 | * |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4485 | * @hide |
| 4486 | */ |
Fabrice Di Meglio | bce84d2 | 2011-06-02 15:57:01 -0700 | [diff] [blame] | 4487 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4488 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"), |
| 4489 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"), |
| 4490 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"), |
| 4491 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE") |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4492 | }) |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4493 | public int getLayoutDirection() { |
| 4494 | return mViewFlags & LAYOUT_DIRECTION_MASK; |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | /** |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 4498 | * Set the layout direction for this view. This will propagate a reset of layout direction |
| 4499 | * resolution to the view's children and resolve layout direction for this view. |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4500 | * |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4501 | * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR}, |
| 4502 | * {@link #LAYOUT_DIRECTION_RTL}, |
| 4503 | * {@link #LAYOUT_DIRECTION_INHERIT} or |
| 4504 | * {@link #LAYOUT_DIRECTION_LOCALE}. |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 4505 | * |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4506 | * @attr ref android.R.styleable#View_layoutDirection |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4507 | * |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4508 | * @hide |
| 4509 | */ |
| 4510 | @RemotableViewMethod |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 4511 | public void setLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 4512 | if (getLayoutDirection() != layoutDirection) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 4513 | resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 4514 | // Setting the flag will also request a layout. |
| 4515 | setFlags(layoutDirection, LAYOUT_DIRECTION_MASK); |
| 4516 | } |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | /** |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4520 | * Returns the resolved layout direction for this view. |
| 4521 | * |
| 4522 | * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns |
| 4523 | * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL. |
| 4524 | * |
| 4525 | * @hide |
| 4526 | */ |
| 4527 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
| 4528 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"), |
| 4529 | @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL") |
| 4530 | }) |
| 4531 | public int getResolvedLayoutDirection() { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 4532 | resolveLayoutDirectionIfNeeded(); |
| 4533 | return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ? |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 4534 | LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR; |
| 4535 | } |
| 4536 | |
| 4537 | /** |
| 4538 | * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from |
| 4539 | * layout attribute and/or the inherited value from the parent.</p> |
| 4540 | * |
| 4541 | * @return true if the layout is right-to-left. |
| 4542 | * |
| 4543 | * @hide |
| 4544 | */ |
| 4545 | @ViewDebug.ExportedProperty(category = "layout") |
| 4546 | public boolean isLayoutRtl() { |
| 4547 | return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 4548 | } |
| 4549 | |
| 4550 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4551 | * If this view doesn't do any drawing on its own, set this flag to |
| 4552 | * allow further optimizations. By default, this flag is not set on |
| 4553 | * View, but could be set on some View subclasses such as ViewGroup. |
| 4554 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 4555 | * Typically, if you override {@link #onDraw(android.graphics.Canvas)} |
| 4556 | * you should clear this flag. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4557 | * |
| 4558 | * @param willNotDraw whether or not this View draw on its own |
| 4559 | */ |
| 4560 | public void setWillNotDraw(boolean willNotDraw) { |
| 4561 | setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK); |
| 4562 | } |
| 4563 | |
| 4564 | /** |
| 4565 | * Returns whether or not this View draws on its own. |
| 4566 | * |
| 4567 | * @return true if this view has nothing to draw, false otherwise |
| 4568 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4569 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4570 | public boolean willNotDraw() { |
| 4571 | return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW; |
| 4572 | } |
| 4573 | |
| 4574 | /** |
| 4575 | * When a View's drawing cache is enabled, drawing is redirected to an |
| 4576 | * offscreen bitmap. Some views, like an ImageView, must be able to |
| 4577 | * bypass this mechanism if they already draw a single bitmap, to avoid |
| 4578 | * unnecessary usage of the memory. |
| 4579 | * |
| 4580 | * @param willNotCacheDrawing true if this view does not cache its |
| 4581 | * drawing, false otherwise |
| 4582 | */ |
| 4583 | public void setWillNotCacheDrawing(boolean willNotCacheDrawing) { |
| 4584 | setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING); |
| 4585 | } |
| 4586 | |
| 4587 | /** |
| 4588 | * Returns whether or not this View can cache its drawing or not. |
| 4589 | * |
| 4590 | * @return true if this view does not cache its drawing, false otherwise |
| 4591 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4592 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4593 | public boolean willNotCacheDrawing() { |
| 4594 | return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING; |
| 4595 | } |
| 4596 | |
| 4597 | /** |
| 4598 | * Indicates whether this view reacts to click events or not. |
| 4599 | * |
| 4600 | * @return true if the view is clickable, false otherwise |
| 4601 | * |
| 4602 | * @see #setClickable(boolean) |
| 4603 | * @attr ref android.R.styleable#View_clickable |
| 4604 | */ |
| 4605 | @ViewDebug.ExportedProperty |
| 4606 | public boolean isClickable() { |
| 4607 | return (mViewFlags & CLICKABLE) == CLICKABLE; |
| 4608 | } |
| 4609 | |
| 4610 | /** |
| 4611 | * Enables or disables click events for this view. When a view |
| 4612 | * is clickable it will change its state to "pressed" on every click. |
| 4613 | * Subclasses should set the view clickable to visually react to |
| 4614 | * user's clicks. |
| 4615 | * |
| 4616 | * @param clickable true to make the view clickable, false otherwise |
| 4617 | * |
| 4618 | * @see #isClickable() |
| 4619 | * @attr ref android.R.styleable#View_clickable |
| 4620 | */ |
| 4621 | public void setClickable(boolean clickable) { |
| 4622 | setFlags(clickable ? CLICKABLE : 0, CLICKABLE); |
| 4623 | } |
| 4624 | |
| 4625 | /** |
| 4626 | * Indicates whether this view reacts to long click events or not. |
| 4627 | * |
| 4628 | * @return true if the view is long clickable, false otherwise |
| 4629 | * |
| 4630 | * @see #setLongClickable(boolean) |
| 4631 | * @attr ref android.R.styleable#View_longClickable |
| 4632 | */ |
| 4633 | public boolean isLongClickable() { |
| 4634 | return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE; |
| 4635 | } |
| 4636 | |
| 4637 | /** |
| 4638 | * Enables or disables long click events for this view. When a view is long |
| 4639 | * clickable it reacts to the user holding down the button for a longer |
| 4640 | * duration than a tap. This event can either launch the listener or a |
| 4641 | * context menu. |
| 4642 | * |
| 4643 | * @param longClickable true to make the view long clickable, false otherwise |
| 4644 | * @see #isLongClickable() |
| 4645 | * @attr ref android.R.styleable#View_longClickable |
| 4646 | */ |
| 4647 | public void setLongClickable(boolean longClickable) { |
| 4648 | setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE); |
| 4649 | } |
| 4650 | |
| 4651 | /** |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 4652 | * Sets the pressed state for this view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | * |
| 4654 | * @see #isClickable() |
| 4655 | * @see #setClickable(boolean) |
| 4656 | * |
| 4657 | * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts |
| 4658 | * the View's internal state from a previously set "pressed" state. |
| 4659 | */ |
| 4660 | public void setPressed(boolean pressed) { |
| 4661 | if (pressed) { |
| 4662 | mPrivateFlags |= PRESSED; |
| 4663 | } else { |
| 4664 | mPrivateFlags &= ~PRESSED; |
| 4665 | } |
| 4666 | refreshDrawableState(); |
| 4667 | dispatchSetPressed(pressed); |
| 4668 | } |
| 4669 | |
| 4670 | /** |
| 4671 | * Dispatch setPressed to all of this View's children. |
| 4672 | * |
| 4673 | * @see #setPressed(boolean) |
| 4674 | * |
| 4675 | * @param pressed The new pressed state |
| 4676 | */ |
| 4677 | protected void dispatchSetPressed(boolean pressed) { |
| 4678 | } |
| 4679 | |
| 4680 | /** |
| 4681 | * Indicates whether the view is currently in pressed state. Unless |
| 4682 | * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter |
| 4683 | * the pressed state. |
| 4684 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 4685 | * @see #setPressed(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | * @see #isClickable() |
| 4687 | * @see #setClickable(boolean) |
| 4688 | * |
| 4689 | * @return true if the view is currently pressed, false otherwise |
| 4690 | */ |
| 4691 | public boolean isPressed() { |
| 4692 | return (mPrivateFlags & PRESSED) == PRESSED; |
| 4693 | } |
| 4694 | |
| 4695 | /** |
| 4696 | * Indicates whether this view will save its state (that is, |
| 4697 | * whether its {@link #onSaveInstanceState} method will be called). |
| 4698 | * |
| 4699 | * @return Returns true if the view state saving is enabled, else false. |
| 4700 | * |
| 4701 | * @see #setSaveEnabled(boolean) |
| 4702 | * @attr ref android.R.styleable#View_saveEnabled |
| 4703 | */ |
| 4704 | public boolean isSaveEnabled() { |
| 4705 | return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED; |
| 4706 | } |
| 4707 | |
| 4708 | /** |
| 4709 | * Controls whether the saving of this view's state is |
| 4710 | * enabled (that is, whether its {@link #onSaveInstanceState} method |
| 4711 | * will be called). Note that even if freezing is enabled, the |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 4712 | * view still must have an id assigned to it (via {@link #setId(int)}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | * for its state to be saved. This flag can only disable the |
| 4714 | * saving of this view; any child views may still have their state saved. |
| 4715 | * |
| 4716 | * @param enabled Set to false to <em>disable</em> state saving, or true |
| 4717 | * (the default) to allow it. |
| 4718 | * |
| 4719 | * @see #isSaveEnabled() |
| 4720 | * @see #setId(int) |
| 4721 | * @see #onSaveInstanceState() |
| 4722 | * @attr ref android.R.styleable#View_saveEnabled |
| 4723 | */ |
| 4724 | public void setSaveEnabled(boolean enabled) { |
| 4725 | setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK); |
| 4726 | } |
| 4727 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 4728 | /** |
| 4729 | * Gets whether the framework should discard touches when the view's |
| 4730 | * window is obscured by another visible window. |
| 4731 | * Refer to the {@link View} security documentation for more details. |
| 4732 | * |
| 4733 | * @return True if touch filtering is enabled. |
| 4734 | * |
| 4735 | * @see #setFilterTouchesWhenObscured(boolean) |
| 4736 | * @attr ref android.R.styleable#View_filterTouchesWhenObscured |
| 4737 | */ |
| 4738 | @ViewDebug.ExportedProperty |
| 4739 | public boolean getFilterTouchesWhenObscured() { |
| 4740 | return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0; |
| 4741 | } |
| 4742 | |
| 4743 | /** |
| 4744 | * Sets whether the framework should discard touches when the view's |
| 4745 | * window is obscured by another visible window. |
| 4746 | * Refer to the {@link View} security documentation for more details. |
| 4747 | * |
| 4748 | * @param enabled True if touch filtering should be enabled. |
| 4749 | * |
| 4750 | * @see #getFilterTouchesWhenObscured |
| 4751 | * @attr ref android.R.styleable#View_filterTouchesWhenObscured |
| 4752 | */ |
| 4753 | public void setFilterTouchesWhenObscured(boolean enabled) { |
| 4754 | setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED, |
| 4755 | FILTER_TOUCHES_WHEN_OBSCURED); |
| 4756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4757 | |
| 4758 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 4759 | * Indicates whether the entire hierarchy under this view will save its |
| 4760 | * state when a state saving traversal occurs from its parent. The default |
| 4761 | * is true; if false, these views will not be saved unless |
| 4762 | * {@link #saveHierarchyState(SparseArray)} is called directly on this view. |
| 4763 | * |
| 4764 | * @return Returns true if the view state saving from parent is enabled, else false. |
| 4765 | * |
| 4766 | * @see #setSaveFromParentEnabled(boolean) |
| 4767 | */ |
| 4768 | public boolean isSaveFromParentEnabled() { |
| 4769 | return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED; |
| 4770 | } |
| 4771 | |
| 4772 | /** |
| 4773 | * Controls whether the entire hierarchy under this view will save its |
| 4774 | * state when a state saving traversal occurs from its parent. The default |
| 4775 | * is true; if false, these views will not be saved unless |
| 4776 | * {@link #saveHierarchyState(SparseArray)} is called directly on this view. |
| 4777 | * |
| 4778 | * @param enabled Set to false to <em>disable</em> state saving, or true |
| 4779 | * (the default) to allow it. |
| 4780 | * |
| 4781 | * @see #isSaveFromParentEnabled() |
| 4782 | * @see #setId(int) |
| 4783 | * @see #onSaveInstanceState() |
| 4784 | */ |
| 4785 | public void setSaveFromParentEnabled(boolean enabled) { |
| 4786 | setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK); |
| 4787 | } |
| 4788 | |
| 4789 | |
| 4790 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4791 | * Returns whether this View is able to take focus. |
| 4792 | * |
| 4793 | * @return True if this view can take focus, or false otherwise. |
| 4794 | * @attr ref android.R.styleable#View_focusable |
| 4795 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4796 | @ViewDebug.ExportedProperty(category = "focus") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4797 | public final boolean isFocusable() { |
| 4798 | return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK); |
| 4799 | } |
| 4800 | |
| 4801 | /** |
| 4802 | * When a view is focusable, it may not want to take focus when in touch mode. |
| 4803 | * For example, a button would like focus when the user is navigating via a D-pad |
| 4804 | * so that the user can click on it, but once the user starts touching the screen, |
| 4805 | * the button shouldn't take focus |
| 4806 | * @return Whether the view is focusable in touch mode. |
| 4807 | * @attr ref android.R.styleable#View_focusableInTouchMode |
| 4808 | */ |
| 4809 | @ViewDebug.ExportedProperty |
| 4810 | public final boolean isFocusableInTouchMode() { |
| 4811 | return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE); |
| 4812 | } |
| 4813 | |
| 4814 | /** |
| 4815 | * Find the nearest view in the specified direction that can take focus. |
| 4816 | * This does not actually give focus to that view. |
| 4817 | * |
| 4818 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 4819 | * |
| 4820 | * @return The nearest focusable in the specified direction, or null if none |
| 4821 | * can be found. |
| 4822 | */ |
| 4823 | public View focusSearch(int direction) { |
| 4824 | if (mParent != null) { |
| 4825 | return mParent.focusSearch(this, direction); |
| 4826 | } else { |
| 4827 | return null; |
| 4828 | } |
| 4829 | } |
| 4830 | |
| 4831 | /** |
| 4832 | * This method is the last chance for the focused view and its ancestors to |
| 4833 | * respond to an arrow key. This is called when the focused view did not |
| 4834 | * consume the key internally, nor could the view system find a new view in |
| 4835 | * the requested direction to give focus to. |
| 4836 | * |
| 4837 | * @param focused The currently focused view. |
| 4838 | * @param direction The direction focus wants to move. One of FOCUS_UP, |
| 4839 | * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT. |
| 4840 | * @return True if the this view consumed this unhandled move. |
| 4841 | */ |
| 4842 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 4843 | return false; |
| 4844 | } |
| 4845 | |
| 4846 | /** |
| 4847 | * If a user manually specified the next view id for a particular direction, |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4848 | * use the root to look up the view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4849 | * @param root The root view of the hierarchy containing this view. |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4850 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, |
| 4851 | * or FOCUS_BACKWARD. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4852 | * @return The user specified next view, or null if there is none. |
| 4853 | */ |
| 4854 | View findUserSetNextFocus(View root, int direction) { |
| 4855 | switch (direction) { |
| 4856 | case FOCUS_LEFT: |
| 4857 | if (mNextFocusLeftId == View.NO_ID) return null; |
| 4858 | return findViewShouldExist(root, mNextFocusLeftId); |
| 4859 | case FOCUS_RIGHT: |
| 4860 | if (mNextFocusRightId == View.NO_ID) return null; |
| 4861 | return findViewShouldExist(root, mNextFocusRightId); |
| 4862 | case FOCUS_UP: |
| 4863 | if (mNextFocusUpId == View.NO_ID) return null; |
| 4864 | return findViewShouldExist(root, mNextFocusUpId); |
| 4865 | case FOCUS_DOWN: |
| 4866 | if (mNextFocusDownId == View.NO_ID) return null; |
| 4867 | return findViewShouldExist(root, mNextFocusDownId); |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 4868 | case FOCUS_FORWARD: |
| 4869 | if (mNextFocusForwardId == View.NO_ID) return null; |
| 4870 | return findViewShouldExist(root, mNextFocusForwardId); |
| 4871 | case FOCUS_BACKWARD: { |
| 4872 | final int id = mID; |
| 4873 | return root.findViewByPredicate(new Predicate<View>() { |
| 4874 | @Override |
| 4875 | public boolean apply(View t) { |
| 4876 | return t.mNextFocusForwardId == id; |
| 4877 | } |
| 4878 | }); |
| 4879 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4880 | } |
| 4881 | return null; |
| 4882 | } |
| 4883 | |
| 4884 | private static View findViewShouldExist(View root, int childViewId) { |
| 4885 | View result = root.findViewById(childViewId); |
| 4886 | if (result == null) { |
| 4887 | Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified " |
| 4888 | + "by user for id " + childViewId); |
| 4889 | } |
| 4890 | return result; |
| 4891 | } |
| 4892 | |
| 4893 | /** |
| 4894 | * Find and return all focusable views that are descendants of this view, |
| 4895 | * possibly including this view if it is focusable itself. |
| 4896 | * |
| 4897 | * @param direction The direction of the focus |
| 4898 | * @return A list of focusable views |
| 4899 | */ |
| 4900 | public ArrayList<View> getFocusables(int direction) { |
| 4901 | ArrayList<View> result = new ArrayList<View>(24); |
| 4902 | addFocusables(result, direction); |
| 4903 | return result; |
| 4904 | } |
| 4905 | |
| 4906 | /** |
| 4907 | * Add any focusable views that are descendants of this view (possibly |
| 4908 | * including this view if it is focusable itself) to views. If we are in touch mode, |
| 4909 | * only add views that are also focusable in touch mode. |
| 4910 | * |
| 4911 | * @param views Focusable views found so far |
| 4912 | * @param direction The direction of the focus |
| 4913 | */ |
| 4914 | public void addFocusables(ArrayList<View> views, int direction) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4915 | addFocusables(views, direction, FOCUSABLES_TOUCH_MODE); |
| 4916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4917 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4918 | /** |
| 4919 | * Adds any focusable views that are descendants of this view (possibly |
| 4920 | * including this view if it is focusable itself) to views. This method |
| 4921 | * adds all focusable views regardless if we are in touch mode or |
| 4922 | * only views focusable in touch mode if we are in touch mode depending on |
| 4923 | * the focusable mode paramater. |
| 4924 | * |
| 4925 | * @param views Focusable views found so far or null if all we are interested is |
| 4926 | * the number of focusables. |
| 4927 | * @param direction The direction of the focus. |
| 4928 | * @param focusableMode The type of focusables to be added. |
| 4929 | * |
| 4930 | * @see #FOCUSABLES_ALL |
| 4931 | * @see #FOCUSABLES_TOUCH_MODE |
| 4932 | */ |
| 4933 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
| 4934 | if (!isFocusable()) { |
| 4935 | return; |
| 4936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4937 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4938 | if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE && |
| 4939 | isInTouchMode() && !isFocusableInTouchMode()) { |
| 4940 | return; |
| 4941 | } |
| 4942 | |
| 4943 | if (views != null) { |
| 4944 | views.add(this); |
| 4945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 4949 | * Finds the Views that contain given text. The containment is case insensitive. |
| 4950 | * As View's text is considered any text content that View renders. |
| 4951 | * |
| 4952 | * @param outViews The output list of matching Views. |
| 4953 | * @param text The text to match against. |
| 4954 | */ |
| 4955 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text) { |
| 4956 | } |
| 4957 | |
| 4958 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4959 | * Find and return all touchable views that are descendants of this view, |
| 4960 | * possibly including this view if it is touchable itself. |
| 4961 | * |
| 4962 | * @return A list of touchable views |
| 4963 | */ |
| 4964 | public ArrayList<View> getTouchables() { |
| 4965 | ArrayList<View> result = new ArrayList<View>(); |
| 4966 | addTouchables(result); |
| 4967 | return result; |
| 4968 | } |
| 4969 | |
| 4970 | /** |
| 4971 | * Add any touchable views that are descendants of this view (possibly |
| 4972 | * including this view if it is touchable itself) to views. |
| 4973 | * |
| 4974 | * @param views Touchable views found so far |
| 4975 | */ |
| 4976 | public void addTouchables(ArrayList<View> views) { |
| 4977 | final int viewFlags = mViewFlags; |
| 4978 | |
| 4979 | if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) |
| 4980 | && (viewFlags & ENABLED_MASK) == ENABLED) { |
| 4981 | views.add(this); |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | /** |
| 4986 | * Call this to try to give focus to a specific view or to one of its |
| 4987 | * descendants. |
| 4988 | * |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 4989 | * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns |
| 4990 | * false), or if it is focusable and it is not focusable in touch mode |
| 4991 | * ({@link #isFocusableInTouchMode}) while the device is in touch mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4992 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 4993 | * See also {@link #focusSearch(int)}, which is what you call to say that you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4994 | * have focus, and you want your parent to look for the next one. |
| 4995 | * |
| 4996 | * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments |
| 4997 | * {@link #FOCUS_DOWN} and <code>null</code>. |
| 4998 | * |
| 4999 | * @return Whether this view or one of its descendants actually took focus. |
| 5000 | */ |
| 5001 | public final boolean requestFocus() { |
| 5002 | return requestFocus(View.FOCUS_DOWN); |
| 5003 | } |
| 5004 | |
| 5005 | |
| 5006 | /** |
| 5007 | * Call this to try to give focus to a specific view or to one of its |
| 5008 | * descendants and give it a hint about what direction focus is heading. |
| 5009 | * |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 5010 | * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns |
| 5011 | * false), or if it is focusable and it is not focusable in touch mode |
| 5012 | * ({@link #isFocusableInTouchMode}) while the device is in touch mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5013 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5014 | * See also {@link #focusSearch(int)}, which is what you call to say that you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5015 | * have focus, and you want your parent to look for the next one. |
| 5016 | * |
| 5017 | * This is equivalent to calling {@link #requestFocus(int, Rect)} with |
| 5018 | * <code>null</code> set for the previously focused rectangle. |
| 5019 | * |
| 5020 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 5021 | * @return Whether this view or one of its descendants actually took focus. |
| 5022 | */ |
| 5023 | public final boolean requestFocus(int direction) { |
| 5024 | return requestFocus(direction, null); |
| 5025 | } |
| 5026 | |
| 5027 | /** |
| 5028 | * Call this to try to give focus to a specific view or to one of its descendants |
| 5029 | * and give it hints about the direction and a specific rectangle that the focus |
| 5030 | * is coming from. The rectangle can help give larger views a finer grained hint |
| 5031 | * about where focus is coming from, and therefore, where to show selection, or |
| 5032 | * forward focus change internally. |
| 5033 | * |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 5034 | * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns |
| 5035 | * false), or if it is focusable and it is not focusable in touch mode |
| 5036 | * ({@link #isFocusableInTouchMode}) while the device is in touch mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5037 | * |
| 5038 | * A View will not take focus if it is not visible. |
| 5039 | * |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 5040 | * A View will not take focus if one of its parents has |
| 5041 | * {@link android.view.ViewGroup#getDescendantFocusability()} equal to |
| 5042 | * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5043 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5044 | * See also {@link #focusSearch(int)}, which is what you call to say that you |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5045 | * have focus, and you want your parent to look for the next one. |
| 5046 | * |
| 5047 | * You may wish to override this method if your custom {@link View} has an internal |
| 5048 | * {@link View} that it wishes to forward the request to. |
| 5049 | * |
| 5050 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 5051 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 5052 | * to give a finer grained hint about where focus is coming from. May be null |
| 5053 | * if there is no hint. |
| 5054 | * @return Whether this view or one of its descendants actually took focus. |
| 5055 | */ |
| 5056 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 5057 | // need to be focusable |
| 5058 | if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE || |
| 5059 | (mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 5060 | return false; |
| 5061 | } |
| 5062 | |
| 5063 | // need to be focusable in touch mode if in touch mode |
| 5064 | if (isInTouchMode() && |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 5065 | (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) { |
| 5066 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | // need to not have any parents blocking us |
| 5070 | if (hasAncestorThatBlocksDescendantFocus()) { |
| 5071 | return false; |
| 5072 | } |
| 5073 | |
| 5074 | handleFocusGainInternal(direction, previouslyFocusedRect); |
| 5075 | return true; |
| 5076 | } |
| 5077 | |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 5078 | /** Gets the ViewAncestor, or null if not attached. */ |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 5079 | /*package*/ ViewRootImpl getViewRootImpl() { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 5080 | View root = getRootView(); |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 5081 | return root != null ? (ViewRootImpl)root.getParent() : null; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 5082 | } |
| 5083 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5084 | /** |
| 5085 | * Call this to try to give focus to a specific view or to one of its descendants. This is a |
| 5086 | * special variant of {@link #requestFocus() } that will allow views that are not focuable in |
| 5087 | * touch mode to request focus when they are touched. |
| 5088 | * |
| 5089 | * @return Whether this view or one of its descendants actually took focus. |
| 5090 | * |
| 5091 | * @see #isInTouchMode() |
| 5092 | * |
| 5093 | */ |
| 5094 | public final boolean requestFocusFromTouch() { |
| 5095 | // Leave touch mode if we need to |
| 5096 | if (isInTouchMode()) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 5097 | ViewRootImpl viewRoot = getViewRootImpl(); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 5098 | if (viewRoot != null) { |
| 5099 | viewRoot.ensureTouchMode(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5100 | } |
| 5101 | } |
| 5102 | return requestFocus(View.FOCUS_DOWN); |
| 5103 | } |
| 5104 | |
| 5105 | /** |
| 5106 | * @return Whether any ancestor of this view blocks descendant focus. |
| 5107 | */ |
| 5108 | private boolean hasAncestorThatBlocksDescendantFocus() { |
| 5109 | ViewParent ancestor = mParent; |
| 5110 | while (ancestor instanceof ViewGroup) { |
| 5111 | final ViewGroup vgAncestor = (ViewGroup) ancestor; |
| 5112 | if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) { |
| 5113 | return true; |
| 5114 | } else { |
| 5115 | ancestor = vgAncestor.getParent(); |
| 5116 | } |
| 5117 | } |
| 5118 | return false; |
| 5119 | } |
| 5120 | |
| 5121 | /** |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 5122 | * @hide |
| 5123 | */ |
| 5124 | public void dispatchStartTemporaryDetach() { |
| 5125 | onStartTemporaryDetach(); |
| 5126 | } |
| 5127 | |
| 5128 | /** |
| 5129 | * This is called when a container is going to temporarily detach a child, with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5130 | * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}. |
| 5131 | * It will either be followed by {@link #onFinishTemporaryDetach()} or |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 5132 | * {@link #onDetachedFromWindow()} when the container is done. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5133 | */ |
| 5134 | public void onStartTemporaryDetach() { |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 5135 | removeUnsetPressCallback(); |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 5136 | mPrivateFlags |= CANCEL_NEXT_UP_EVENT; |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | /** |
| 5140 | * @hide |
| 5141 | */ |
| 5142 | public void dispatchFinishTemporaryDetach() { |
| 5143 | onFinishTemporaryDetach(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5144 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5146 | /** |
| 5147 | * Called after {@link #onStartTemporaryDetach} when the container is done |
| 5148 | * changing the view. |
| 5149 | */ |
| 5150 | public void onFinishTemporaryDetach() { |
| 5151 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5153 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 5154 | * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState} |
| 5155 | * for this view's window. Returns null if the view is not currently attached |
| 5156 | * to the window. Normally you will not need to use this directly, but |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5157 | * just use the standard high-level event callbacks like |
| 5158 | * {@link #onKeyDown(int, KeyEvent)}. |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 5159 | */ |
| 5160 | public KeyEvent.DispatcherState getKeyDispatcherState() { |
| 5161 | return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null; |
| 5162 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 5163 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 5164 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5165 | * Dispatch a key event before it is processed by any input method |
| 5166 | * associated with the view hierarchy. This can be used to intercept |
| 5167 | * key events in special situations before the IME consumes them; a |
| 5168 | * typical example would be handling the BACK key to update the application's |
| 5169 | * UI instead of allowing the IME to see it and close itself. |
| 5170 | * |
| 5171 | * @param event The key event to be dispatched. |
| 5172 | * @return True if the event was handled, false otherwise. |
| 5173 | */ |
| 5174 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
| 5175 | return onKeyPreIme(event.getKeyCode(), event); |
| 5176 | } |
| 5177 | |
| 5178 | /** |
| 5179 | * Dispatch a key event to the next view on the focus path. This path runs |
| 5180 | * from the top of the view tree down to the currently focused view. If this |
| 5181 | * view has focus, it will dispatch to itself. Otherwise it will dispatch |
| 5182 | * the next node down the focus path. This method also fires any key |
| 5183 | * listeners. |
| 5184 | * |
| 5185 | * @param event The key event to be dispatched. |
| 5186 | * @return True if the event was handled, false otherwise. |
| 5187 | */ |
| 5188 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 5189 | if (mInputEventConsistencyVerifier != null) { |
| 5190 | mInputEventConsistencyVerifier.onKeyEvent(event, 0); |
| 5191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5192 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 5193 | // Give any attached key listener a first crack at the event. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 5194 | //noinspection SimplifiableIfStatement |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5195 | if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED |
| 5196 | && mOnKeyListener.onKey(this, event.getKeyCode(), event)) { |
| 5197 | return true; |
| 5198 | } |
| 5199 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 5200 | if (event.dispatch(this, mAttachInfo != null |
| 5201 | ? mAttachInfo.mKeyDispatchState : null, this)) { |
| 5202 | return true; |
| 5203 | } |
| 5204 | |
| 5205 | if (mInputEventConsistencyVerifier != null) { |
| 5206 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 0); |
| 5207 | } |
| 5208 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | /** |
| 5212 | * Dispatches a key shortcut event. |
| 5213 | * |
| 5214 | * @param event The key event to be dispatched. |
| 5215 | * @return True if the event was handled by the view, false otherwise. |
| 5216 | */ |
| 5217 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 5218 | return onKeyShortcut(event.getKeyCode(), event); |
| 5219 | } |
| 5220 | |
| 5221 | /** |
| 5222 | * Pass the touch screen motion event down to the target view, or this |
| 5223 | * view if it is the target. |
| 5224 | * |
| 5225 | * @param event The motion event to be dispatched. |
| 5226 | * @return True if the event was handled by the view, false otherwise. |
| 5227 | */ |
| 5228 | public boolean dispatchTouchEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 5229 | if (mInputEventConsistencyVerifier != null) { |
| 5230 | mInputEventConsistencyVerifier.onTouchEvent(event, 0); |
| 5231 | } |
| 5232 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 5233 | if (onFilterTouchEventForSecurity(event)) { |
| 5234 | //noinspection SimplifiableIfStatement |
| 5235 | if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && |
| 5236 | mOnTouchListener.onTouch(this, event)) { |
| 5237 | return true; |
| 5238 | } |
| 5239 | |
| 5240 | if (onTouchEvent(event)) { |
| 5241 | return true; |
| 5242 | } |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 5245 | if (mInputEventConsistencyVerifier != null) { |
| 5246 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5247 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 5248 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | /** |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 5252 | * Filter the touch event to apply security policies. |
| 5253 | * |
| 5254 | * @param event The motion event to be filtered. |
| 5255 | * @return True if the event should be dispatched, false if the event should be dropped. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 5256 | * |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 5257 | * @see #getFilterTouchesWhenObscured |
| 5258 | */ |
| 5259 | public boolean onFilterTouchEventForSecurity(MotionEvent event) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 5260 | //noinspection RedundantIfStatement |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 5261 | if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0 |
| 5262 | && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { |
| 5263 | // Window is obscured, drop this touch. |
| 5264 | return false; |
| 5265 | } |
| 5266 | return true; |
| 5267 | } |
| 5268 | |
| 5269 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5270 | * Pass a trackball motion event down to the focused view. |
| 5271 | * |
| 5272 | * @param event The motion event to be dispatched. |
| 5273 | * @return True if the event was handled by the view, false otherwise. |
| 5274 | */ |
| 5275 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 5276 | if (mInputEventConsistencyVerifier != null) { |
| 5277 | mInputEventConsistencyVerifier.onTrackballEvent(event, 0); |
| 5278 | } |
| 5279 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 5280 | return onTrackballEvent(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5281 | } |
| 5282 | |
| 5283 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5284 | * Dispatch a generic motion event. |
| 5285 | * <p> |
| 5286 | * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER} |
| 5287 | * are delivered to the view under the pointer. All other generic motion events are |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5288 | * delivered to the focused view. Hover events are handled specially and are delivered |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5289 | * to {@link #onHoverEvent(MotionEvent)}. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5290 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5291 | * |
| 5292 | * @param event The motion event to be dispatched. |
| 5293 | * @return True if the event was handled by the view, false otherwise. |
| 5294 | */ |
| 5295 | public boolean dispatchGenericMotionEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 5296 | if (mInputEventConsistencyVerifier != null) { |
| 5297 | mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0); |
| 5298 | } |
| 5299 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5300 | final int source = event.getSource(); |
| 5301 | if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 5302 | final int action = event.getAction(); |
| 5303 | if (action == MotionEvent.ACTION_HOVER_ENTER |
| 5304 | || action == MotionEvent.ACTION_HOVER_MOVE |
| 5305 | || action == MotionEvent.ACTION_HOVER_EXIT) { |
| 5306 | if (dispatchHoverEvent(event)) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 5307 | // For compatibility with existing applications that handled HOVER_MOVE |
| 5308 | // events in onGenericMotionEvent, dispatch the event there. The |
| 5309 | // onHoverEvent method did not exist at the time. |
| 5310 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 5311 | dispatchGenericMotionEventInternal(event); |
| 5312 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5313 | return true; |
| 5314 | } |
| 5315 | } else if (dispatchGenericPointerEvent(event)) { |
| 5316 | return true; |
| 5317 | } |
| 5318 | } else if (dispatchGenericFocusedEvent(event)) { |
| 5319 | return true; |
| 5320 | } |
| 5321 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 5322 | if (dispatchGenericMotionEventInternal(event)) { |
| 5323 | return true; |
| 5324 | } |
| 5325 | |
| 5326 | if (mInputEventConsistencyVerifier != null) { |
| 5327 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 0); |
| 5328 | } |
| 5329 | return false; |
| 5330 | } |
| 5331 | |
| 5332 | private boolean dispatchGenericMotionEventInternal(MotionEvent event) { |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 5333 | //noinspection SimplifiableIfStatement |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5334 | if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED |
| 5335 | && mOnGenericMotionListener.onGenericMotion(this, event)) { |
| 5336 | return true; |
| 5337 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 5338 | |
| 5339 | if (onGenericMotionEvent(event)) { |
| 5340 | return true; |
| 5341 | } |
| 5342 | |
| 5343 | if (mInputEventConsistencyVerifier != null) { |
| 5344 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 0); |
| 5345 | } |
| 5346 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5350 | * Dispatch a hover event. |
| 5351 | * <p> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5352 | * Do not call this method directly. |
| 5353 | * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5354 | * </p> |
| 5355 | * |
| 5356 | * @param event The motion event to be dispatched. |
| 5357 | * @return True if the event was handled by the view, false otherwise. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5358 | */ |
| 5359 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 5360 | switch (event.getAction()) { |
| 5361 | case MotionEvent.ACTION_HOVER_ENTER: |
| 5362 | if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) { |
| 5363 | mSendingHoverAccessibilityEvents = true; |
| 5364 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER); |
| 5365 | } |
| 5366 | break; |
| 5367 | case MotionEvent.ACTION_HOVER_EXIT: |
| 5368 | if (mSendingHoverAccessibilityEvents) { |
| 5369 | mSendingHoverAccessibilityEvents = false; |
| 5370 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT); |
| 5371 | } |
| 5372 | break; |
| 5373 | } |
| 5374 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 5375 | //noinspection SimplifiableIfStatement |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 5376 | if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED |
| 5377 | && mOnHoverListener.onHover(this, event)) { |
| 5378 | return true; |
| 5379 | } |
| 5380 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5381 | return onHoverEvent(event); |
| 5382 | } |
| 5383 | |
| 5384 | /** |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 5385 | * Returns true if the view has a child to which it has recently sent |
| 5386 | * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and |
| 5387 | * it does not have a hovered child, then it must be the innermost hovered view. |
| 5388 | * @hide |
| 5389 | */ |
| 5390 | protected boolean hasHoveredChild() { |
| 5391 | return false; |
| 5392 | } |
| 5393 | |
| 5394 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5395 | * Dispatch a generic motion event to the view under the first pointer. |
| 5396 | * <p> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5397 | * Do not call this method directly. |
| 5398 | * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5399 | * </p> |
| 5400 | * |
| 5401 | * @param event The motion event to be dispatched. |
| 5402 | * @return True if the event was handled by the view, false otherwise. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5403 | */ |
| 5404 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 5405 | return false; |
| 5406 | } |
| 5407 | |
| 5408 | /** |
| 5409 | * Dispatch a generic motion event to the currently focused view. |
| 5410 | * <p> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5411 | * Do not call this method directly. |
| 5412 | * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5413 | * </p> |
| 5414 | * |
| 5415 | * @param event The motion event to be dispatched. |
| 5416 | * @return True if the event was handled by the view, false otherwise. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5417 | */ |
| 5418 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
| 5419 | return false; |
| 5420 | } |
| 5421 | |
| 5422 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5423 | * Dispatch a pointer event. |
| 5424 | * <p> |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5425 | * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all |
| 5426 | * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns |
| 5427 | * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5428 | * and should not be expected to handle other pointing device features. |
| 5429 | * </p> |
| 5430 | * |
| 5431 | * @param event The motion event to be dispatched. |
| 5432 | * @return True if the event was handled by the view, false otherwise. |
| 5433 | * @hide |
| 5434 | */ |
| 5435 | public final boolean dispatchPointerEvent(MotionEvent event) { |
| 5436 | if (event.isTouchEvent()) { |
| 5437 | return dispatchTouchEvent(event); |
| 5438 | } else { |
| 5439 | return dispatchGenericMotionEvent(event); |
| 5440 | } |
| 5441 | } |
| 5442 | |
| 5443 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5444 | * Called when the window containing this view gains or loses window focus. |
| 5445 | * ViewGroups should override to route to their children. |
| 5446 | * |
| 5447 | * @param hasFocus True if the window containing this view now has focus, |
| 5448 | * false otherwise. |
| 5449 | */ |
| 5450 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 5451 | onWindowFocusChanged(hasFocus); |
| 5452 | } |
| 5453 | |
| 5454 | /** |
| 5455 | * Called when the window containing this view gains or loses focus. Note |
| 5456 | * that this is separate from view focus: to receive key events, both |
| 5457 | * your view and its window must have focus. If a window is displayed |
| 5458 | * on top of yours that takes input focus, then your own window will lose |
| 5459 | * focus but the view focus will remain unchanged. |
| 5460 | * |
| 5461 | * @param hasWindowFocus True if the window containing this view now has |
| 5462 | * focus, false otherwise. |
| 5463 | */ |
| 5464 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 5465 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 5466 | if (!hasWindowFocus) { |
| 5467 | if (isPressed()) { |
| 5468 | setPressed(false); |
| 5469 | } |
| 5470 | if (imm != null && (mPrivateFlags & FOCUSED) != 0) { |
| 5471 | imm.focusOut(this); |
| 5472 | } |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 5473 | removeLongPressCallback(); |
Tony Wu | 26edf20 | 2010-09-13 19:54:00 +0800 | [diff] [blame] | 5474 | removeTapCallback(); |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 5475 | onFocusLost(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5476 | } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) { |
| 5477 | imm.focusIn(this); |
| 5478 | } |
| 5479 | refreshDrawableState(); |
| 5480 | } |
| 5481 | |
| 5482 | /** |
| 5483 | * Returns true if this view is in a window that currently has window focus. |
| 5484 | * Note that this is not the same as the view itself having focus. |
| 5485 | * |
| 5486 | * @return True if this view is in a window that currently has window focus. |
| 5487 | */ |
| 5488 | public boolean hasWindowFocus() { |
| 5489 | return mAttachInfo != null && mAttachInfo.mHasWindowFocus; |
| 5490 | } |
| 5491 | |
| 5492 | /** |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 5493 | * Dispatch a view visibility change down the view hierarchy. |
| 5494 | * ViewGroups should override to route to their children. |
| 5495 | * @param changedView The view whose visibility changed. Could be 'this' or |
| 5496 | * an ancestor view. |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 5497 | * @param visibility The new visibility of changedView: {@link #VISIBLE}, |
| 5498 | * {@link #INVISIBLE} or {@link #GONE}. |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 5499 | */ |
| 5500 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 5501 | onVisibilityChanged(changedView, visibility); |
| 5502 | } |
| 5503 | |
| 5504 | /** |
| 5505 | * Called when the visibility of the view or an ancestor of the view is changed. |
| 5506 | * @param changedView The view whose visibility changed. Could be 'this' or |
| 5507 | * an ancestor view. |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 5508 | * @param visibility The new visibility of changedView: {@link #VISIBLE}, |
| 5509 | * {@link #INVISIBLE} or {@link #GONE}. |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 5510 | */ |
| 5511 | protected void onVisibilityChanged(View changedView, int visibility) { |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 5512 | if (visibility == VISIBLE) { |
| 5513 | if (mAttachInfo != null) { |
| 5514 | initialAwakenScrollBars(); |
| 5515 | } else { |
| 5516 | mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH; |
| 5517 | } |
| 5518 | } |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | /** |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 5522 | * Dispatch a hint about whether this view is displayed. For instance, when |
| 5523 | * a View moves out of the screen, it might receives a display hint indicating |
| 5524 | * the view is not displayed. Applications should not <em>rely</em> on this hint |
| 5525 | * as there is no guarantee that they will receive one. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 5526 | * |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 5527 | * @param hint A hint about whether or not this view is displayed: |
| 5528 | * {@link #VISIBLE} or {@link #INVISIBLE}. |
| 5529 | */ |
| 5530 | public void dispatchDisplayHint(int hint) { |
| 5531 | onDisplayHint(hint); |
| 5532 | } |
| 5533 | |
| 5534 | /** |
| 5535 | * Gives this view a hint about whether is displayed or not. For instance, when |
| 5536 | * a View moves out of the screen, it might receives a display hint indicating |
| 5537 | * the view is not displayed. Applications should not <em>rely</em> on this hint |
| 5538 | * as there is no guarantee that they will receive one. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 5539 | * |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 5540 | * @param hint A hint about whether or not this view is displayed: |
| 5541 | * {@link #VISIBLE} or {@link #INVISIBLE}. |
| 5542 | */ |
| 5543 | protected void onDisplayHint(int hint) { |
| 5544 | } |
| 5545 | |
| 5546 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5547 | * Dispatch a window visibility change down the view hierarchy. |
| 5548 | * ViewGroups should override to route to their children. |
| 5549 | * |
| 5550 | * @param visibility The new visibility of the window. |
| 5551 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5552 | * @see #onWindowVisibilityChanged(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5553 | */ |
| 5554 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 5555 | onWindowVisibilityChanged(visibility); |
| 5556 | } |
| 5557 | |
| 5558 | /** |
| 5559 | * Called when the window containing has change its visibility |
| 5560 | * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note |
| 5561 | * that this tells you whether or not your window is being made visible |
| 5562 | * to the window manager; this does <em>not</em> tell you whether or not |
| 5563 | * your window is obscured by other windows on the screen, even if it |
| 5564 | * is itself visible. |
| 5565 | * |
| 5566 | * @param visibility The new visibility of the window. |
| 5567 | */ |
| 5568 | protected void onWindowVisibilityChanged(int visibility) { |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 5569 | if (visibility == VISIBLE) { |
| 5570 | initialAwakenScrollBars(); |
| 5571 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | /** |
| 5575 | * Returns the current visibility of the window this view is attached to |
| 5576 | * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}). |
| 5577 | * |
| 5578 | * @return Returns the current visibility of the view's window. |
| 5579 | */ |
| 5580 | public int getWindowVisibility() { |
| 5581 | return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE; |
| 5582 | } |
| 5583 | |
| 5584 | /** |
| 5585 | * Retrieve the overall visible display size in which the window this view is |
| 5586 | * attached to has been positioned in. This takes into account screen |
| 5587 | * decorations above the window, for both cases where the window itself |
| 5588 | * is being position inside of them or the window is being placed under |
| 5589 | * then and covered insets are used for the window to position its content |
| 5590 | * inside. In effect, this tells you the available area where content can |
| 5591 | * be placed and remain visible to users. |
| 5592 | * |
| 5593 | * <p>This function requires an IPC back to the window manager to retrieve |
| 5594 | * the requested information, so should not be used in performance critical |
| 5595 | * code like drawing. |
| 5596 | * |
| 5597 | * @param outRect Filled in with the visible display frame. If the view |
| 5598 | * is not attached to a window, this is simply the raw display size. |
| 5599 | */ |
| 5600 | public void getWindowVisibleDisplayFrame(Rect outRect) { |
| 5601 | if (mAttachInfo != null) { |
| 5602 | try { |
| 5603 | mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect); |
| 5604 | } catch (RemoteException e) { |
| 5605 | return; |
| 5606 | } |
| 5607 | // XXX This is really broken, and probably all needs to be done |
| 5608 | // in the window manager, and we need to know more about whether |
| 5609 | // we want the area behind or in front of the IME. |
| 5610 | final Rect insets = mAttachInfo.mVisibleInsets; |
| 5611 | outRect.left += insets.left; |
| 5612 | outRect.top += insets.top; |
| 5613 | outRect.right -= insets.right; |
| 5614 | outRect.bottom -= insets.bottom; |
| 5615 | return; |
| 5616 | } |
| 5617 | Display d = WindowManagerImpl.getDefault().getDefaultDisplay(); |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 5618 | d.getRectSize(outRect); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5619 | } |
| 5620 | |
| 5621 | /** |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 5622 | * Dispatch a notification about a resource configuration change down |
| 5623 | * the view hierarchy. |
| 5624 | * ViewGroups should override to route to their children. |
| 5625 | * |
| 5626 | * @param newConfig The new resource configuration. |
| 5627 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 5628 | * @see #onConfigurationChanged(android.content.res.Configuration) |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 5629 | */ |
| 5630 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 5631 | onConfigurationChanged(newConfig); |
| 5632 | } |
| 5633 | |
| 5634 | /** |
| 5635 | * Called when the current configuration of the resources being used |
| 5636 | * by the application have changed. You can use this to decide when |
| 5637 | * to reload resources that can changed based on orientation and other |
| 5638 | * configuration characterstics. You only need to use this if you are |
| 5639 | * not relying on the normal {@link android.app.Activity} mechanism of |
| 5640 | * recreating the activity instance upon a configuration change. |
| 5641 | * |
| 5642 | * @param newConfig The new resource configuration. |
| 5643 | */ |
| 5644 | protected void onConfigurationChanged(Configuration newConfig) { |
| 5645 | } |
| 5646 | |
| 5647 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5648 | * Private function to aggregate all per-view attributes in to the view |
| 5649 | * root. |
| 5650 | */ |
| 5651 | void dispatchCollectViewAttributes(int visibility) { |
| 5652 | performCollectViewAttributes(visibility); |
| 5653 | } |
| 5654 | |
| 5655 | void performCollectViewAttributes(int visibility) { |
Romain Guy | d30b36d | 2011-01-26 10:54:43 -0800 | [diff] [blame] | 5656 | if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 5657 | if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) { |
| 5658 | mAttachInfo.mKeepScreenOn = true; |
| 5659 | } |
| 5660 | mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility; |
| 5661 | if (mOnSystemUiVisibilityChangeListener != null) { |
| 5662 | mAttachInfo.mHasSystemUiListeners = true; |
| 5663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | void needGlobalAttributesUpdate(boolean force) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 5668 | final AttachInfo ai = mAttachInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5669 | if (ai != null) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 5670 | if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0) |
| 5671 | || ai.mHasSystemUiListeners) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | ai.mRecomputeGlobalAttributes = true; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * Returns whether the device is currently in touch mode. Touch mode is entered |
| 5679 | * once the user begins interacting with the device by touch, and affects various |
| 5680 | * things like whether focus is always visible to the user. |
| 5681 | * |
| 5682 | * @return Whether the device is in touch mode. |
| 5683 | */ |
| 5684 | @ViewDebug.ExportedProperty |
| 5685 | public boolean isInTouchMode() { |
| 5686 | if (mAttachInfo != null) { |
| 5687 | return mAttachInfo.mInTouchMode; |
| 5688 | } else { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 5689 | return ViewRootImpl.isInTouchMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | /** |
| 5694 | * Returns the context the view is running in, through which it can |
| 5695 | * access the current theme, resources, etc. |
| 5696 | * |
| 5697 | * @return The view's Context. |
| 5698 | */ |
| 5699 | @ViewDebug.CapturedViewProperty |
| 5700 | public final Context getContext() { |
| 5701 | return mContext; |
| 5702 | } |
| 5703 | |
| 5704 | /** |
| 5705 | * Handle a key event before it is processed by any input method |
| 5706 | * associated with the view hierarchy. This can be used to intercept |
| 5707 | * key events in special situations before the IME consumes them; a |
| 5708 | * typical example would be handling the BACK key to update the application's |
| 5709 | * UI instead of allowing the IME to see it and close itself. |
| 5710 | * |
| 5711 | * @param keyCode The value in event.getKeyCode(). |
| 5712 | * @param event Description of the key event. |
| 5713 | * @return If you handled the event, return true. If you want to allow the |
| 5714 | * event to be handled by the next receiver, return false. |
| 5715 | */ |
| 5716 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
| 5717 | return false; |
| 5718 | } |
| 5719 | |
| 5720 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 5721 | * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent) |
| 5722 | * KeyEvent.Callback.onKeyDown()}: perform press of the view |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5723 | * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER} |
| 5724 | * is released, if the view is enabled and clickable. |
| 5725 | * |
| 5726 | * @param keyCode A key code that represents the button pressed, from |
| 5727 | * {@link android.view.KeyEvent}. |
| 5728 | * @param event The KeyEvent object that defines the button action. |
| 5729 | */ |
| 5730 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 5731 | boolean result = false; |
| 5732 | |
| 5733 | switch (keyCode) { |
| 5734 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 5735 | case KeyEvent.KEYCODE_ENTER: { |
| 5736 | if ((mViewFlags & ENABLED_MASK) == DISABLED) { |
| 5737 | return true; |
| 5738 | } |
| 5739 | // Long clickable items don't necessarily have to be clickable |
| 5740 | if (((mViewFlags & CLICKABLE) == CLICKABLE || |
| 5741 | (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) && |
| 5742 | (event.getRepeatCount() == 0)) { |
| 5743 | setPressed(true); |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 5744 | checkForLongClick(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5745 | return true; |
| 5746 | } |
| 5747 | break; |
| 5748 | } |
| 5749 | } |
| 5750 | return result; |
| 5751 | } |
| 5752 | |
| 5753 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 5754 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 5755 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 5756 | * the event). |
| 5757 | */ |
| 5758 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 5759 | return false; |
| 5760 | } |
| 5761 | |
| 5762 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 5763 | * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent) |
| 5764 | * KeyEvent.Callback.onKeyUp()}: perform clicking of the view |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5765 | * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or |
| 5766 | * {@link KeyEvent#KEYCODE_ENTER} is released. |
| 5767 | * |
| 5768 | * @param keyCode A key code that represents the button pressed, from |
| 5769 | * {@link android.view.KeyEvent}. |
| 5770 | * @param event The KeyEvent object that defines the button action. |
| 5771 | */ |
| 5772 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 5773 | boolean result = false; |
| 5774 | |
| 5775 | switch (keyCode) { |
| 5776 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 5777 | case KeyEvent.KEYCODE_ENTER: { |
| 5778 | if ((mViewFlags & ENABLED_MASK) == DISABLED) { |
| 5779 | return true; |
| 5780 | } |
| 5781 | if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) { |
| 5782 | setPressed(false); |
| 5783 | |
| 5784 | if (!mHasPerformedLongPress) { |
| 5785 | // This is a tap, so remove the longpress check |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 5786 | removeLongPressCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5787 | |
| 5788 | result = performClick(); |
| 5789 | } |
| 5790 | } |
| 5791 | break; |
| 5792 | } |
| 5793 | } |
| 5794 | return result; |
| 5795 | } |
| 5796 | |
| 5797 | /** |
| 5798 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 5799 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 5800 | * the event). |
| 5801 | * |
| 5802 | * @param keyCode A key code that represents the button pressed, from |
| 5803 | * {@link android.view.KeyEvent}. |
| 5804 | * @param repeatCount The number of times the action was made. |
| 5805 | * @param event The KeyEvent object that defines the button action. |
| 5806 | */ |
| 5807 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 5808 | return false; |
| 5809 | } |
| 5810 | |
| 5811 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 5812 | * Called on the focused view when a key shortcut event is not handled. |
| 5813 | * Override this method to implement local key shortcuts for the View. |
| 5814 | * Key shortcuts can also be implemented by setting the |
| 5815 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5816 | * |
| 5817 | * @param keyCode The value in event.getKeyCode(). |
| 5818 | * @param event Description of the key event. |
| 5819 | * @return If you handled the event, return true. If you want to allow the |
| 5820 | * event to be handled by the next receiver, return false. |
| 5821 | */ |
| 5822 | public boolean onKeyShortcut(int keyCode, KeyEvent event) { |
| 5823 | return false; |
| 5824 | } |
| 5825 | |
| 5826 | /** |
| 5827 | * Check whether the called view is a text editor, in which case it |
| 5828 | * would make sense to automatically display a soft input window for |
| 5829 | * it. Subclasses should override this if they implement |
| 5830 | * {@link #onCreateInputConnection(EditorInfo)} to return true if |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5831 | * a call on that method would return a non-null InputConnection, and |
| 5832 | * they are really a first-class editor that the user would normally |
| 5833 | * start typing on when the go into a window containing your view. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5834 | * |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5835 | * <p>The default implementation always returns false. This does |
| 5836 | * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)} |
| 5837 | * will not be called or the user can not otherwise perform edits on your |
| 5838 | * view; it is just a hint to the system that this is not the primary |
| 5839 | * purpose of this view. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5840 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5841 | * @return Returns true if this view is a text editor, else false. |
| 5842 | */ |
| 5843 | public boolean onCheckIsTextEditor() { |
| 5844 | return false; |
| 5845 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5847 | /** |
| 5848 | * Create a new InputConnection for an InputMethod to interact |
| 5849 | * with the view. The default implementation returns null, since it doesn't |
| 5850 | * support input methods. You can override this to implement such support. |
| 5851 | * This is only needed for views that take focus and text input. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5852 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5853 | * <p>When implementing this, you probably also want to implement |
| 5854 | * {@link #onCheckIsTextEditor()} to indicate you will return a |
| 5855 | * non-null InputConnection. |
| 5856 | * |
| 5857 | * @param outAttrs Fill in with attribute information about the connection. |
| 5858 | */ |
| 5859 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 5860 | return null; |
| 5861 | } |
| 5862 | |
| 5863 | /** |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5864 | * Called by the {@link android.view.inputmethod.InputMethodManager} |
| 5865 | * when a view who is not the current |
| 5866 | * input connection target is trying to make a call on the manager. The |
| 5867 | * default implementation returns false; you can override this to return |
| 5868 | * true for certain views if you are performing InputConnection proxying |
| 5869 | * to them. |
| 5870 | * @param view The View that is making the InputMethodManager call. |
| 5871 | * @return Return true to allow the call, false to reject. |
| 5872 | */ |
| 5873 | public boolean checkInputConnectionProxy(View view) { |
| 5874 | return false; |
| 5875 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5876 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 5877 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5878 | * Show the context menu for this view. It is not safe to hold on to the |
| 5879 | * menu after returning from this method. |
| 5880 | * |
Gilles Debunne | f788a9f | 2010-07-22 10:17:23 -0700 | [diff] [blame] | 5881 | * You should normally not overload this method. Overload |
| 5882 | * {@link #onCreateContextMenu(ContextMenu)} or define an |
| 5883 | * {@link OnCreateContextMenuListener} to add items to the context menu. |
| 5884 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5885 | * @param menu The context menu to populate |
| 5886 | */ |
| 5887 | public void createContextMenu(ContextMenu menu) { |
| 5888 | ContextMenuInfo menuInfo = getContextMenuInfo(); |
| 5889 | |
| 5890 | // Sets the current menu info so all items added to menu will have |
| 5891 | // my extra info set. |
| 5892 | ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo); |
| 5893 | |
| 5894 | onCreateContextMenu(menu); |
| 5895 | if (mOnCreateContextMenuListener != null) { |
| 5896 | mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo); |
| 5897 | } |
| 5898 | |
| 5899 | // Clear the extra information so subsequent items that aren't mine don't |
| 5900 | // have my extra info. |
| 5901 | ((MenuBuilder)menu).setCurrentMenuInfo(null); |
| 5902 | |
| 5903 | if (mParent != null) { |
| 5904 | mParent.createContextMenu(menu); |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | /** |
| 5909 | * Views should implement this if they have extra information to associate |
| 5910 | * with the context menu. The return result is supplied as a parameter to |
| 5911 | * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} |
| 5912 | * callback. |
| 5913 | * |
| 5914 | * @return Extra information about the item for which the context menu |
| 5915 | * should be shown. This information will vary across different |
| 5916 | * subclasses of View. |
| 5917 | */ |
| 5918 | protected ContextMenuInfo getContextMenuInfo() { |
| 5919 | return null; |
| 5920 | } |
| 5921 | |
| 5922 | /** |
| 5923 | * Views should implement this if the view itself is going to add items to |
| 5924 | * the context menu. |
| 5925 | * |
| 5926 | * @param menu the context menu to populate |
| 5927 | */ |
| 5928 | protected void onCreateContextMenu(ContextMenu menu) { |
| 5929 | } |
| 5930 | |
| 5931 | /** |
| 5932 | * Implement this method to handle trackball motion events. The |
| 5933 | * <em>relative</em> movement of the trackball since the last event |
| 5934 | * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and |
| 5935 | * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so |
| 5936 | * that a movement of 1 corresponds to the user pressing one DPAD key (so |
| 5937 | * they will often be fractional values, representing the more fine-grained |
| 5938 | * movement information available from a trackball). |
| 5939 | * |
| 5940 | * @param event The motion event. |
| 5941 | * @return True if the event was handled, false otherwise. |
| 5942 | */ |
| 5943 | public boolean onTrackballEvent(MotionEvent event) { |
| 5944 | return false; |
| 5945 | } |
| 5946 | |
| 5947 | /** |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5948 | * Implement this method to handle generic motion events. |
| 5949 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5950 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 5951 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5952 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 5953 | * the class of input that was received. Implementations of this method |
| 5954 | * must examine the bits in the source before processing the event. |
| 5955 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5956 | * </p><p> |
| 5957 | * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER} |
| 5958 | * are delivered to the view under the pointer. All other generic motion events are |
| 5959 | * delivered to the focused view. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5960 | * </p> |
| 5961 | * <code> |
| 5962 | * public boolean onGenericMotionEvent(MotionEvent event) { |
| 5963 | * if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 5964 | * if (event.getAction() == MotionEvent.ACTION_MOVE) { |
| 5965 | * // process the joystick movement... |
| 5966 | * return true; |
| 5967 | * } |
| 5968 | * } |
| 5969 | * if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 5970 | * switch (event.getAction()) { |
| 5971 | * case MotionEvent.ACTION_HOVER_MOVE: |
| 5972 | * // process the mouse hover movement... |
| 5973 | * return true; |
| 5974 | * case MotionEvent.ACTION_SCROLL: |
| 5975 | * // process the scroll wheel movement... |
| 5976 | * return true; |
| 5977 | * } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5978 | * } |
| 5979 | * return super.onGenericMotionEvent(event); |
| 5980 | * } |
| 5981 | * </code> |
| 5982 | * |
| 5983 | * @param event The generic motion event being processed. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5984 | * @return True if the event was handled, false otherwise. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5985 | */ |
| 5986 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 5987 | return false; |
| 5988 | } |
| 5989 | |
| 5990 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 5991 | * Implement this method to handle hover events. |
| 5992 | * <p> |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 5993 | * This method is called whenever a pointer is hovering into, over, or out of the |
| 5994 | * bounds of a view and the view is not currently being touched. |
| 5995 | * Hover events are represented as pointer events with action |
| 5996 | * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE}, |
| 5997 | * or {@link MotionEvent#ACTION_HOVER_EXIT}. |
| 5998 | * </p> |
| 5999 | * <ul> |
| 6000 | * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER} |
| 6001 | * when the pointer enters the bounds of the view.</li> |
| 6002 | * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE} |
| 6003 | * when the pointer has already entered the bounds of the view and has moved.</li> |
| 6004 | * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT} |
| 6005 | * when the pointer has exited the bounds of the view or when the pointer is |
| 6006 | * about to go down due to a button click, tap, or similar user action that |
| 6007 | * causes the view to be touched.</li> |
| 6008 | * </ul> |
| 6009 | * <p> |
| 6010 | * The view should implement this method to return true to indicate that it is |
| 6011 | * handling the hover event, such as by changing its drawable state. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6012 | * </p><p> |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6013 | * The default implementation calls {@link #setHovered} to update the hovered state |
| 6014 | * of the view when a hover enter or hover exit event is received, if the view |
| 6015 | * is enabled and is clickable. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6016 | * </p> |
| 6017 | * |
| 6018 | * @param event The motion event that describes the hover. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6019 | * @return True if the view handled the hover event. |
| 6020 | * |
| 6021 | * @see #isHovered |
| 6022 | * @see #setHovered |
| 6023 | * @see #onHoverChanged |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6024 | */ |
| 6025 | public boolean onHoverEvent(MotionEvent event) { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 6026 | if (isHoverable()) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6027 | switch (event.getAction()) { |
| 6028 | case MotionEvent.ACTION_HOVER_ENTER: |
| 6029 | setHovered(true); |
| 6030 | break; |
| 6031 | case MotionEvent.ACTION_HOVER_EXIT: |
| 6032 | setHovered(false); |
| 6033 | break; |
| 6034 | } |
| 6035 | return true; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6036 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 6037 | return false; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | /** |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 6041 | * Returns true if the view should handle {@link #onHoverEvent} |
| 6042 | * by calling {@link #setHovered} to change its hovered state. |
| 6043 | * |
| 6044 | * @return True if the view is hoverable. |
| 6045 | */ |
| 6046 | private boolean isHoverable() { |
| 6047 | final int viewFlags = mViewFlags; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 6048 | //noinspection SimplifiableIfStatement |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 6049 | if ((viewFlags & ENABLED_MASK) == DISABLED) { |
| 6050 | return false; |
| 6051 | } |
| 6052 | |
| 6053 | return (viewFlags & CLICKABLE) == CLICKABLE |
| 6054 | || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE; |
| 6055 | } |
| 6056 | |
| 6057 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6058 | * Returns true if the view is currently hovered. |
| 6059 | * |
| 6060 | * @return True if the view is currently hovered. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6061 | * |
| 6062 | * @see #setHovered |
| 6063 | * @see #onHoverChanged |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6064 | */ |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6065 | @ViewDebug.ExportedProperty |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6066 | public boolean isHovered() { |
| 6067 | return (mPrivateFlags & HOVERED) != 0; |
| 6068 | } |
| 6069 | |
| 6070 | /** |
| 6071 | * Sets whether the view is currently hovered. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6072 | * <p> |
| 6073 | * Calling this method also changes the drawable state of the view. This |
| 6074 | * enables the view to react to hover by using different drawable resources |
| 6075 | * to change its appearance. |
| 6076 | * </p><p> |
| 6077 | * The {@link #onHoverChanged} method is called when the hovered state changes. |
| 6078 | * </p> |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6079 | * |
| 6080 | * @param hovered True if the view is hovered. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6081 | * |
| 6082 | * @see #isHovered |
| 6083 | * @see #onHoverChanged |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6084 | */ |
| 6085 | public void setHovered(boolean hovered) { |
| 6086 | if (hovered) { |
| 6087 | if ((mPrivateFlags & HOVERED) == 0) { |
| 6088 | mPrivateFlags |= HOVERED; |
| 6089 | refreshDrawableState(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6090 | onHoverChanged(true); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6091 | } |
| 6092 | } else { |
| 6093 | if ((mPrivateFlags & HOVERED) != 0) { |
| 6094 | mPrivateFlags &= ~HOVERED; |
| 6095 | refreshDrawableState(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6096 | onHoverChanged(false); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 6097 | } |
| 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6102 | * Implement this method to handle hover state changes. |
| 6103 | * <p> |
| 6104 | * This method is called whenever the hover state changes as a result of a |
| 6105 | * call to {@link #setHovered}. |
| 6106 | * </p> |
| 6107 | * |
| 6108 | * @param hovered The current hover state, as returned by {@link #isHovered}. |
| 6109 | * |
| 6110 | * @see #isHovered |
| 6111 | * @see #setHovered |
| 6112 | */ |
| 6113 | public void onHoverChanged(boolean hovered) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 6114 | } |
| 6115 | |
| 6116 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6117 | * Implement this method to handle touch screen motion events. |
| 6118 | * |
| 6119 | * @param event The motion event. |
| 6120 | * @return True if the event was handled, false otherwise. |
| 6121 | */ |
| 6122 | public boolean onTouchEvent(MotionEvent event) { |
| 6123 | final int viewFlags = mViewFlags; |
| 6124 | |
| 6125 | if ((viewFlags & ENABLED_MASK) == DISABLED) { |
Svetoslav Ganov | 77b80c0 | 2011-03-15 20:52:58 -0700 | [diff] [blame] | 6126 | if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) { |
| 6127 | mPrivateFlags &= ~PRESSED; |
| 6128 | refreshDrawableState(); |
| 6129 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6130 | // A disabled view that is clickable still consumes the touch |
| 6131 | // events, it just doesn't respond to them. |
| 6132 | return (((viewFlags & CLICKABLE) == CLICKABLE || |
| 6133 | (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)); |
| 6134 | } |
| 6135 | |
| 6136 | if (mTouchDelegate != null) { |
| 6137 | if (mTouchDelegate.onTouchEvent(event)) { |
| 6138 | return true; |
| 6139 | } |
| 6140 | } |
| 6141 | |
| 6142 | if (((viewFlags & CLICKABLE) == CLICKABLE || |
| 6143 | (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { |
| 6144 | switch (event.getAction()) { |
| 6145 | case MotionEvent.ACTION_UP: |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6146 | boolean prepressed = (mPrivateFlags & PREPRESSED) != 0; |
| 6147 | if ((mPrivateFlags & PRESSED) != 0 || prepressed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6148 | // take focus if we don't have it already and we should in |
| 6149 | // touch mode. |
| 6150 | boolean focusTaken = false; |
| 6151 | if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { |
| 6152 | focusTaken = requestFocus(); |
| 6153 | } |
| 6154 | |
Dianne Hackborn | be1f622 | 2011-01-20 15:24:28 -0800 | [diff] [blame] | 6155 | if (prepressed) { |
| 6156 | // The button is being released before we actually |
| 6157 | // showed it as pressed. Make it show the pressed |
| 6158 | // state now (before scheduling the click) to ensure |
| 6159 | // the user sees it. |
| 6160 | mPrivateFlags |= PRESSED; |
| 6161 | refreshDrawableState(); |
| 6162 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 6163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6164 | if (!mHasPerformedLongPress) { |
| 6165 | // This is a tap, so remove the longpress check |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 6166 | removeLongPressCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6167 | |
| 6168 | // Only perform take click actions if we were in the pressed state |
| 6169 | if (!focusTaken) { |
Adam Powell | a35d768 | 2010-03-12 14:48:13 -0800 | [diff] [blame] | 6170 | // Use a Runnable and post this rather than calling |
| 6171 | // performClick directly. This lets other visual state |
| 6172 | // of the view update before click actions start. |
| 6173 | if (mPerformClick == null) { |
| 6174 | mPerformClick = new PerformClick(); |
| 6175 | } |
| 6176 | if (!post(mPerformClick)) { |
| 6177 | performClick(); |
| 6178 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | if (mUnsetPressedState == null) { |
| 6183 | mUnsetPressedState = new UnsetPressedState(); |
| 6184 | } |
| 6185 | |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6186 | if (prepressed) { |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6187 | postDelayed(mUnsetPressedState, |
| 6188 | ViewConfiguration.getPressedStateDuration()); |
| 6189 | } else if (!post(mUnsetPressedState)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6190 | // If the post failed, unpress right now |
| 6191 | mUnsetPressedState.run(); |
| 6192 | } |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6193 | removeTapCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6194 | } |
| 6195 | break; |
| 6196 | |
| 6197 | case MotionEvent.ACTION_DOWN: |
Adam Powell | 3b02339 | 2010-03-11 16:30:28 -0800 | [diff] [blame] | 6198 | mHasPerformedLongPress = false; |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6199 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6200 | if (performButtonActionOnTouchDown(event)) { |
| 6201 | break; |
| 6202 | } |
| 6203 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6204 | // Walk up the hierarchy to determine if we're inside a scrolling container. |
| 6205 | boolean isInScrollingContainer = false; |
| 6206 | ViewParent p = getParent(); |
| 6207 | while (p != null && p instanceof ViewGroup) { |
| 6208 | if (((ViewGroup) p).shouldDelayChildPressedState()) { |
| 6209 | isInScrollingContainer = true; |
| 6210 | break; |
| 6211 | } |
| 6212 | p = p.getParent(); |
| 6213 | } |
| 6214 | |
| 6215 | // For views inside a scrolling container, delay the pressed feedback for |
| 6216 | // a short period in case this is a scroll. |
| 6217 | if (isInScrollingContainer) { |
| 6218 | mPrivateFlags |= PREPRESSED; |
| 6219 | if (mPendingCheckForTap == null) { |
| 6220 | mPendingCheckForTap = new CheckForTap(); |
| 6221 | } |
| 6222 | postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); |
| 6223 | } else { |
| 6224 | // Not inside a scrolling container, so show the feedback right away |
| 6225 | mPrivateFlags |= PRESSED; |
| 6226 | refreshDrawableState(); |
| 6227 | checkForLongClick(0); |
| 6228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6229 | break; |
| 6230 | |
| 6231 | case MotionEvent.ACTION_CANCEL: |
| 6232 | mPrivateFlags &= ~PRESSED; |
| 6233 | refreshDrawableState(); |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6234 | removeTapCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6235 | break; |
| 6236 | |
| 6237 | case MotionEvent.ACTION_MOVE: |
| 6238 | final int x = (int) event.getX(); |
| 6239 | final int y = (int) event.getY(); |
| 6240 | |
| 6241 | // Be lenient about moving outside of buttons |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6242 | if (!pointInView(x, y, mTouchSlop)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6243 | // Outside button |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6244 | removeTapCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6245 | if ((mPrivateFlags & PRESSED) != 0) { |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6246 | // Remove any future long press/tap checks |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 6247 | removeLongPressCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6248 | |
| 6249 | // Need to switch from pressed to not pressed |
| 6250 | mPrivateFlags &= ~PRESSED; |
| 6251 | refreshDrawableState(); |
| 6252 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6253 | } |
| 6254 | break; |
| 6255 | } |
| 6256 | return true; |
| 6257 | } |
| 6258 | |
| 6259 | return false; |
| 6260 | } |
| 6261 | |
| 6262 | /** |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 6263 | * Remove the longpress detection timer. |
| 6264 | */ |
| 6265 | private void removeLongPressCallback() { |
| 6266 | if (mPendingCheckForLongPress != null) { |
| 6267 | removeCallbacks(mPendingCheckForLongPress); |
| 6268 | } |
| 6269 | } |
Adam Powell | 3cb8b63 | 2011-01-21 15:34:14 -0800 | [diff] [blame] | 6270 | |
| 6271 | /** |
| 6272 | * Remove the pending click action |
| 6273 | */ |
| 6274 | private void removePerformClickCallback() { |
| 6275 | if (mPerformClick != null) { |
| 6276 | removeCallbacks(mPerformClick); |
| 6277 | } |
| 6278 | } |
| 6279 | |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6280 | /** |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 6281 | * Remove the prepress detection timer. |
| 6282 | */ |
| 6283 | private void removeUnsetPressCallback() { |
| 6284 | if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) { |
| 6285 | setPressed(false); |
| 6286 | removeCallbacks(mUnsetPressedState); |
| 6287 | } |
| 6288 | } |
| 6289 | |
| 6290 | /** |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 6291 | * Remove the tap detection timer. |
| 6292 | */ |
| 6293 | private void removeTapCallback() { |
| 6294 | if (mPendingCheckForTap != null) { |
| 6295 | mPrivateFlags &= ~PREPRESSED; |
| 6296 | removeCallbacks(mPendingCheckForTap); |
| 6297 | } |
| 6298 | } |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 6299 | |
| 6300 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6301 | * Cancels a pending long press. Your subclass can use this if you |
| 6302 | * want the context menu to come up if the user presses and holds |
| 6303 | * at the same place, but you don't want it to come up if they press |
| 6304 | * and then move around enough to cause scrolling. |
| 6305 | */ |
| 6306 | public void cancelLongPress() { |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 6307 | removeLongPressCallback(); |
Adam Powell | 732ebb1 | 2010-02-02 15:28:14 -0800 | [diff] [blame] | 6308 | |
| 6309 | /* |
| 6310 | * The prepressed state handled by the tap callback is a display |
| 6311 | * construct, but the tap callback will post a long press callback |
| 6312 | * less its own timeout. Remove it here. |
| 6313 | */ |
| 6314 | removeTapCallback(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | /** |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 6318 | * Remove the pending callback for sending a |
| 6319 | * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event. |
| 6320 | */ |
| 6321 | private void removeSendViewScrolledAccessibilityEventCallback() { |
| 6322 | if (mSendViewScrolledAccessibilityEvent != null) { |
| 6323 | removeCallbacks(mSendViewScrolledAccessibilityEvent); |
| 6324 | } |
| 6325 | } |
| 6326 | |
| 6327 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6328 | * Sets the TouchDelegate for this View. |
| 6329 | */ |
| 6330 | public void setTouchDelegate(TouchDelegate delegate) { |
| 6331 | mTouchDelegate = delegate; |
| 6332 | } |
| 6333 | |
| 6334 | /** |
| 6335 | * Gets the TouchDelegate for this View. |
| 6336 | */ |
| 6337 | public TouchDelegate getTouchDelegate() { |
| 6338 | return mTouchDelegate; |
| 6339 | } |
| 6340 | |
| 6341 | /** |
| 6342 | * Set flags controlling behavior of this view. |
| 6343 | * |
| 6344 | * @param flags Constant indicating the value which should be set |
| 6345 | * @param mask Constant indicating the bit range that should be changed |
| 6346 | */ |
| 6347 | void setFlags(int flags, int mask) { |
| 6348 | int old = mViewFlags; |
| 6349 | mViewFlags = (mViewFlags & ~mask) | (flags & mask); |
| 6350 | |
| 6351 | int changed = mViewFlags ^ old; |
| 6352 | if (changed == 0) { |
| 6353 | return; |
| 6354 | } |
| 6355 | int privateFlags = mPrivateFlags; |
| 6356 | |
| 6357 | /* Check if the FOCUSABLE bit has changed */ |
| 6358 | if (((changed & FOCUSABLE_MASK) != 0) && |
| 6359 | ((privateFlags & HAS_BOUNDS) !=0)) { |
| 6360 | if (((old & FOCUSABLE_MASK) == FOCUSABLE) |
| 6361 | && ((privateFlags & FOCUSED) != 0)) { |
| 6362 | /* Give up focus if we are no longer focusable */ |
| 6363 | clearFocus(); |
| 6364 | } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE) |
| 6365 | && ((privateFlags & FOCUSED) == 0)) { |
| 6366 | /* |
| 6367 | * Tell the view system that we are now available to take focus |
| 6368 | * if no one else already has it. |
| 6369 | */ |
| 6370 | if (mParent != null) mParent.focusableViewAvailable(this); |
| 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | if ((flags & VISIBILITY_MASK) == VISIBLE) { |
| 6375 | if ((changed & VISIBILITY_MASK) != 0) { |
| 6376 | /* |
| 6377 | * If this view is becoming visible, set the DRAWN flag so that |
| 6378 | * the next invalidate() will not be skipped. |
| 6379 | */ |
| 6380 | mPrivateFlags |= DRAWN; |
| 6381 | |
| 6382 | needGlobalAttributesUpdate(true); |
| 6383 | |
| 6384 | // a view becoming visible is worth notifying the parent |
| 6385 | // about in case nothing has focus. even if this specific view |
| 6386 | // isn't focusable, it may contain something that is, so let |
| 6387 | // the root view try to give this focus if nothing else does. |
| 6388 | if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) { |
| 6389 | mParent.focusableViewAvailable(this); |
| 6390 | } |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | /* Check if the GONE bit has changed */ |
| 6395 | if ((changed & GONE) != 0) { |
| 6396 | needGlobalAttributesUpdate(false); |
| 6397 | requestLayout(); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6398 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6399 | |
Romain Guy | ecd80ee | 2009-12-03 17:13:02 -0800 | [diff] [blame] | 6400 | if (((mViewFlags & VISIBILITY_MASK) == GONE)) { |
| 6401 | if (hasFocus()) clearFocus(); |
| 6402 | destroyDrawingCache(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6403 | } |
| 6404 | if (mAttachInfo != null) { |
| 6405 | mAttachInfo.mViewVisibilityChanged = true; |
| 6406 | } |
| 6407 | } |
| 6408 | |
| 6409 | /* Check if the VISIBLE bit has changed */ |
| 6410 | if ((changed & INVISIBLE) != 0) { |
| 6411 | needGlobalAttributesUpdate(false); |
Chet Haase | c8a9a70 | 2011-06-17 12:13:42 -0700 | [diff] [blame] | 6412 | /* |
| 6413 | * If this view is becoming invisible, set the DRAWN flag so that |
| 6414 | * the next invalidate() will not be skipped. |
| 6415 | */ |
| 6416 | mPrivateFlags |= DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6417 | |
| 6418 | if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) { |
| 6419 | // root view becoming invisible shouldn't clear focus |
| 6420 | if (getRootView() != this) { |
| 6421 | clearFocus(); |
| 6422 | } |
| 6423 | } |
| 6424 | if (mAttachInfo != null) { |
| 6425 | mAttachInfo.mViewVisibilityChanged = true; |
| 6426 | } |
| 6427 | } |
| 6428 | |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 6429 | if ((changed & VISIBILITY_MASK) != 0) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 6430 | if (mParent instanceof ViewGroup) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6431 | ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK)); |
| 6432 | ((View) mParent).invalidate(true); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 6433 | } |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 6434 | dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK)); |
| 6435 | } |
| 6436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6437 | if ((changed & WILL_NOT_CACHE_DRAWING) != 0) { |
| 6438 | destroyDrawingCache(); |
| 6439 | } |
| 6440 | |
| 6441 | if ((changed & DRAWING_CACHE_ENABLED) != 0) { |
| 6442 | destroyDrawingCache(); |
| 6443 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6444 | invalidateParentCaches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) { |
| 6448 | destroyDrawingCache(); |
| 6449 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
| 6450 | } |
| 6451 | |
| 6452 | if ((changed & DRAW_MASK) != 0) { |
| 6453 | if ((mViewFlags & WILL_NOT_DRAW) != 0) { |
| 6454 | if (mBGDrawable != null) { |
| 6455 | mPrivateFlags &= ~SKIP_DRAW; |
| 6456 | mPrivateFlags |= ONLY_DRAWS_BACKGROUND; |
| 6457 | } else { |
| 6458 | mPrivateFlags |= SKIP_DRAW; |
| 6459 | } |
| 6460 | } else { |
| 6461 | mPrivateFlags &= ~SKIP_DRAW; |
| 6462 | } |
| 6463 | requestLayout(); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6464 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6465 | } |
| 6466 | |
| 6467 | if ((changed & KEEP_SCREEN_ON) != 0) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 6468 | if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6469 | mParent.recomputeViewAttributes(this); |
| 6470 | } |
| 6471 | } |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 6472 | |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 6473 | if ((changed & LAYOUT_DIRECTION_MASK) != 0) { |
Cibu Johny | 7632cb9 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 6474 | requestLayout(); |
| 6475 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | /** |
| 6479 | * Change the view's z order in the tree, so it's on top of other sibling |
| 6480 | * views |
| 6481 | */ |
| 6482 | public void bringToFront() { |
| 6483 | if (mParent != null) { |
| 6484 | mParent.bringChildToFront(this); |
| 6485 | } |
| 6486 | } |
| 6487 | |
| 6488 | /** |
| 6489 | * This is called in response to an internal scroll in this view (i.e., the |
| 6490 | * view scrolled its own contents). This is typically as a result of |
| 6491 | * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been |
| 6492 | * called. |
| 6493 | * |
| 6494 | * @param l Current horizontal scroll origin. |
| 6495 | * @param t Current vertical scroll origin. |
| 6496 | * @param oldl Previous horizontal scroll origin. |
| 6497 | * @param oldt Previous vertical scroll origin. |
| 6498 | */ |
| 6499 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 6500 | if (AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 6501 | postSendViewScrolledAccessibilityEventCallback(); |
| 6502 | } |
| 6503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6504 | mBackgroundSizeChanged = true; |
| 6505 | |
| 6506 | final AttachInfo ai = mAttachInfo; |
| 6507 | if (ai != null) { |
| 6508 | ai.mViewScrollChanged = true; |
| 6509 | } |
| 6510 | } |
| 6511 | |
| 6512 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6513 | * Interface definition for a callback to be invoked when the layout bounds of a view |
| 6514 | * changes due to layout processing. |
| 6515 | */ |
| 6516 | public interface OnLayoutChangeListener { |
| 6517 | /** |
| 6518 | * Called when the focus state of a view has changed. |
| 6519 | * |
| 6520 | * @param v The view whose state has changed. |
| 6521 | * @param left The new value of the view's left property. |
| 6522 | * @param top The new value of the view's top property. |
| 6523 | * @param right The new value of the view's right property. |
| 6524 | * @param bottom The new value of the view's bottom property. |
| 6525 | * @param oldLeft The previous value of the view's left property. |
| 6526 | * @param oldTop The previous value of the view's top property. |
| 6527 | * @param oldRight The previous value of the view's right property. |
| 6528 | * @param oldBottom The previous value of the view's bottom property. |
| 6529 | */ |
| 6530 | void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 6531 | int oldLeft, int oldTop, int oldRight, int oldBottom); |
| 6532 | } |
| 6533 | |
| 6534 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6535 | * This is called during layout when the size of this view has changed. If |
| 6536 | * you were just added to the view hierarchy, you're called with the old |
| 6537 | * values of 0. |
| 6538 | * |
| 6539 | * @param w Current width of this view. |
| 6540 | * @param h Current height of this view. |
| 6541 | * @param oldw Old width of this view. |
| 6542 | * @param oldh Old height of this view. |
| 6543 | */ |
| 6544 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 6545 | } |
| 6546 | |
| 6547 | /** |
| 6548 | * Called by draw to draw the child views. This may be overridden |
| 6549 | * by derived classes to gain control just before its children are drawn |
| 6550 | * (but after its own view has been drawn). |
| 6551 | * @param canvas the canvas on which to draw the view |
| 6552 | */ |
| 6553 | protected void dispatchDraw(Canvas canvas) { |
| 6554 | } |
| 6555 | |
| 6556 | /** |
| 6557 | * Gets the parent of this view. Note that the parent is a |
| 6558 | * ViewParent and not necessarily a View. |
| 6559 | * |
| 6560 | * @return Parent of this view. |
| 6561 | */ |
| 6562 | public final ViewParent getParent() { |
| 6563 | return mParent; |
| 6564 | } |
| 6565 | |
| 6566 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6567 | * Set the horizontal scrolled position of your view. This will cause a call to |
| 6568 | * {@link #onScrollChanged(int, int, int, int)} and the view will be |
| 6569 | * invalidated. |
| 6570 | * @param value the x position to scroll to |
| 6571 | */ |
| 6572 | public void setScrollX(int value) { |
| 6573 | scrollTo(value, mScrollY); |
| 6574 | } |
| 6575 | |
| 6576 | /** |
| 6577 | * Set the vertical scrolled position of your view. This will cause a call to |
| 6578 | * {@link #onScrollChanged(int, int, int, int)} and the view will be |
| 6579 | * invalidated. |
| 6580 | * @param value the y position to scroll to |
| 6581 | */ |
| 6582 | public void setScrollY(int value) { |
| 6583 | scrollTo(mScrollX, value); |
| 6584 | } |
| 6585 | |
| 6586 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6587 | * Return the scrolled left position of this view. This is the left edge of |
| 6588 | * the displayed part of your view. You do not need to draw any pixels |
| 6589 | * farther left, since those are outside of the frame of your view on |
| 6590 | * screen. |
| 6591 | * |
| 6592 | * @return The left edge of the displayed part of your view, in pixels. |
| 6593 | */ |
| 6594 | public final int getScrollX() { |
| 6595 | return mScrollX; |
| 6596 | } |
| 6597 | |
| 6598 | /** |
| 6599 | * Return the scrolled top position of this view. This is the top edge of |
| 6600 | * the displayed part of your view. You do not need to draw any pixels above |
| 6601 | * it, since those are outside of the frame of your view on screen. |
| 6602 | * |
| 6603 | * @return The top edge of the displayed part of your view, in pixels. |
| 6604 | */ |
| 6605 | public final int getScrollY() { |
| 6606 | return mScrollY; |
| 6607 | } |
| 6608 | |
| 6609 | /** |
| 6610 | * Return the width of the your view. |
| 6611 | * |
| 6612 | * @return The width of your view, in pixels. |
| 6613 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6614 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6615 | public final int getWidth() { |
| 6616 | return mRight - mLeft; |
| 6617 | } |
| 6618 | |
| 6619 | /** |
| 6620 | * Return the height of your view. |
| 6621 | * |
| 6622 | * @return The height of your view, in pixels. |
| 6623 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6624 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6625 | public final int getHeight() { |
| 6626 | return mBottom - mTop; |
| 6627 | } |
| 6628 | |
| 6629 | /** |
| 6630 | * Return the visible drawing bounds of your view. Fills in the output |
| 6631 | * rectangle with the values from getScrollX(), getScrollY(), |
| 6632 | * getWidth(), and getHeight(). |
| 6633 | * |
| 6634 | * @param outRect The (scrolled) drawing bounds of the view. |
| 6635 | */ |
| 6636 | public void getDrawingRect(Rect outRect) { |
| 6637 | outRect.left = mScrollX; |
| 6638 | outRect.top = mScrollY; |
| 6639 | outRect.right = mScrollX + (mRight - mLeft); |
| 6640 | outRect.bottom = mScrollY + (mBottom - mTop); |
| 6641 | } |
| 6642 | |
| 6643 | /** |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6644 | * Like {@link #getMeasuredWidthAndState()}, but only returns the |
| 6645 | * raw width component (that is the result is masked by |
| 6646 | * {@link #MEASURED_SIZE_MASK}). |
| 6647 | * |
| 6648 | * @return The raw measured width of this view. |
| 6649 | */ |
| 6650 | public final int getMeasuredWidth() { |
| 6651 | return mMeasuredWidth & MEASURED_SIZE_MASK; |
| 6652 | } |
| 6653 | |
| 6654 | /** |
| 6655 | * Return the full width measurement information for this view as computed |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 6656 | * by the most recent call to {@link #measure(int, int)}. This result is a bit mask |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6657 | * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6658 | * This should be used during measurement and layout calculations only. Use |
| 6659 | * {@link #getWidth()} to see how wide a view is after layout. |
| 6660 | * |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6661 | * @return The measured width of this view as a bit mask. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6662 | */ |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6663 | public final int getMeasuredWidthAndState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6664 | return mMeasuredWidth; |
| 6665 | } |
| 6666 | |
| 6667 | /** |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6668 | * Like {@link #getMeasuredHeightAndState()}, but only returns the |
| 6669 | * raw width component (that is the result is masked by |
| 6670 | * {@link #MEASURED_SIZE_MASK}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6671 | * |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6672 | * @return The raw measured height of this view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6673 | */ |
| 6674 | public final int getMeasuredHeight() { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6675 | return mMeasuredHeight & MEASURED_SIZE_MASK; |
| 6676 | } |
| 6677 | |
| 6678 | /** |
| 6679 | * Return the full height measurement information for this view as computed |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 6680 | * by the most recent call to {@link #measure(int, int)}. This result is a bit mask |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6681 | * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}. |
| 6682 | * This should be used during measurement and layout calculations only. Use |
| 6683 | * {@link #getHeight()} to see how wide a view is after layout. |
| 6684 | * |
| 6685 | * @return The measured width of this view as a bit mask. |
| 6686 | */ |
| 6687 | public final int getMeasuredHeightAndState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6688 | return mMeasuredHeight; |
| 6689 | } |
| 6690 | |
| 6691 | /** |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 6692 | * Return only the state bits of {@link #getMeasuredWidthAndState()} |
| 6693 | * and {@link #getMeasuredHeightAndState()}, combined into one integer. |
| 6694 | * The width component is in the regular bits {@link #MEASURED_STATE_MASK} |
| 6695 | * and the height component is at the shifted bits |
| 6696 | * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}. |
| 6697 | */ |
| 6698 | public final int getMeasuredState() { |
| 6699 | return (mMeasuredWidth&MEASURED_STATE_MASK) |
| 6700 | | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT) |
| 6701 | & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT)); |
| 6702 | } |
| 6703 | |
| 6704 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6705 | * The transform matrix of this view, which is calculated based on the current |
| 6706 | * roation, scale, and pivot properties. |
| 6707 | * |
| 6708 | * @see #getRotation() |
| 6709 | * @see #getScaleX() |
| 6710 | * @see #getScaleY() |
| 6711 | * @see #getPivotX() |
| 6712 | * @see #getPivotY() |
| 6713 | * @return The current transform matrix for the view |
| 6714 | */ |
| 6715 | public Matrix getMatrix() { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 6716 | updateMatrix(); |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 6717 | return mMatrix; |
| 6718 | } |
| 6719 | |
| 6720 | /** |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6721 | * Utility function to determine if the value is far enough away from zero to be |
| 6722 | * considered non-zero. |
| 6723 | * @param value A floating point value to check for zero-ness |
| 6724 | * @return whether the passed-in value is far enough away from zero to be considered non-zero |
| 6725 | */ |
| 6726 | private static boolean nonzero(float value) { |
| 6727 | return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON); |
| 6728 | } |
| 6729 | |
| 6730 | /** |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 6731 | * Returns true if the transform matrix is the identity matrix. |
| 6732 | * Recomputes the matrix if necessary. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 6733 | * |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 6734 | * @return True if the transform matrix is the identity matrix, false otherwise. |
| 6735 | */ |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 6736 | final boolean hasIdentityMatrix() { |
| 6737 | updateMatrix(); |
| 6738 | return mMatrixIsIdentity; |
| 6739 | } |
| 6740 | |
| 6741 | /** |
| 6742 | * Recomputes the transform matrix if necessary. |
| 6743 | */ |
Romain Guy | 2fe9a8f | 2010-10-04 20:17:01 -0700 | [diff] [blame] | 6744 | private void updateMatrix() { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6745 | if (mMatrixDirty) { |
| 6746 | // transform-related properties have changed since the last time someone |
| 6747 | // asked for the matrix; recalculate it with the current values |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6748 | |
| 6749 | // Figure out if we need to update the pivot point |
| 6750 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 6751 | if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) { |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6752 | mPrevWidth = mRight - mLeft; |
| 6753 | mPrevHeight = mBottom - mTop; |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 6754 | mPivotX = mPrevWidth / 2f; |
| 6755 | mPivotY = mPrevHeight / 2f; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6756 | } |
| 6757 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6758 | mMatrix.reset(); |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6759 | if (!nonzero(mRotationX) && !nonzero(mRotationY)) { |
| 6760 | mMatrix.setTranslate(mTranslationX, mTranslationY); |
| 6761 | mMatrix.preRotate(mRotation, mPivotX, mPivotY); |
| 6762 | mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY); |
| 6763 | } else { |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6764 | if (mCamera == null) { |
| 6765 | mCamera = new Camera(); |
| 6766 | matrix3D = new Matrix(); |
| 6767 | } |
| 6768 | mCamera.save(); |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6769 | mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY); |
Romain Guy | 47b8ade | 2011-02-23 19:46:33 -0800 | [diff] [blame] | 6770 | mCamera.rotate(mRotationX, mRotationY, -mRotation); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6771 | mCamera.getMatrix(matrix3D); |
| 6772 | matrix3D.preTranslate(-mPivotX, -mPivotY); |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6773 | matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6774 | mMatrix.postConcat(matrix3D); |
| 6775 | mCamera.restore(); |
| 6776 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6777 | mMatrixDirty = false; |
| 6778 | mMatrixIsIdentity = mMatrix.isIdentity(); |
| 6779 | mInverseMatrixDirty = true; |
| 6780 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /** |
| 6784 | * Utility method to retrieve the inverse of the current mMatrix property. |
| 6785 | * We cache the matrix to avoid recalculating it when transform properties |
| 6786 | * have not changed. |
| 6787 | * |
| 6788 | * @return The inverse of the current matrix of this view. |
| 6789 | */ |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 6790 | final Matrix getInverseMatrix() { |
| 6791 | updateMatrix(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6792 | if (mInverseMatrixDirty) { |
| 6793 | if (mInverseMatrix == null) { |
| 6794 | mInverseMatrix = new Matrix(); |
| 6795 | } |
| 6796 | mMatrix.invert(mInverseMatrix); |
| 6797 | mInverseMatrixDirty = false; |
| 6798 | } |
| 6799 | return mInverseMatrix; |
| 6800 | } |
| 6801 | |
| 6802 | /** |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6803 | * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which |
| 6804 | * views are drawn) from the camera to this view. The camera's distance |
| 6805 | * affects 3D transformations, for instance rotations around the X and Y |
| 6806 | * axis. If the rotationX or rotationY properties are changed and this view is |
| 6807 | * large (more than half the size of the screen), it is recommended to always |
| 6808 | * use a camera distance that's greater than the height (X axis rotation) or |
| 6809 | * the width (Y axis rotation) of this view.</p> |
| 6810 | * |
| 6811 | * <p>The distance of the camera from the view plane can have an affect on the |
| 6812 | * perspective distortion of the view when it is rotated around the x or y axis. |
| 6813 | * For example, a large distance will result in a large viewing angle, and there |
| 6814 | * will not be much perspective distortion of the view as it rotates. A short |
| 6815 | * distance may cause much more perspective distortion upon rotation, and can |
| 6816 | * also result in some drawing artifacts if the rotated view ends up partially |
| 6817 | * behind the camera (which is why the recommendation is to use a distance at |
| 6818 | * least as far as the size of the view, if the view is to be rotated.)</p> |
| 6819 | * |
| 6820 | * <p>The distance is expressed in "depth pixels." The default distance depends |
| 6821 | * on the screen density. For instance, on a medium density display, the |
| 6822 | * default distance is 1280. On a high density display, the default distance |
| 6823 | * is 1920.</p> |
| 6824 | * |
| 6825 | * <p>If you want to specify a distance that leads to visually consistent |
| 6826 | * results across various densities, use the following formula:</p> |
| 6827 | * <pre> |
| 6828 | * float scale = context.getResources().getDisplayMetrics().density; |
| 6829 | * view.setCameraDistance(distance * scale); |
| 6830 | * </pre> |
| 6831 | * |
| 6832 | * <p>The density scale factor of a high density display is 1.5, |
| 6833 | * and 1920 = 1280 * 1.5.</p> |
| 6834 | * |
| 6835 | * @param distance The distance in "depth pixels", if negative the opposite |
| 6836 | * value is used |
| 6837 | * |
| 6838 | * @see #setRotationX(float) |
| 6839 | * @see #setRotationY(float) |
| 6840 | */ |
| 6841 | public void setCameraDistance(float distance) { |
| 6842 | invalidateParentCaches(); |
| 6843 | invalidate(false); |
| 6844 | |
| 6845 | final float dpi = mResources.getDisplayMetrics().densityDpi; |
| 6846 | if (mCamera == null) { |
| 6847 | mCamera = new Camera(); |
| 6848 | matrix3D = new Matrix(); |
| 6849 | } |
| 6850 | |
| 6851 | mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi); |
| 6852 | mMatrixDirty = true; |
| 6853 | |
| 6854 | invalidate(false); |
| 6855 | } |
| 6856 | |
| 6857 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6858 | * The degrees that the view is rotated around the pivot point. |
| 6859 | * |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6860 | * @see #setRotation(float) |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6861 | * @see #getPivotX() |
| 6862 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6863 | * |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6864 | * @return The degrees of rotation. |
| 6865 | */ |
| 6866 | public float getRotation() { |
| 6867 | return mRotation; |
| 6868 | } |
| 6869 | |
| 6870 | /** |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6871 | * Sets the degrees that the view is rotated around the pivot point. Increasing values |
| 6872 | * result in clockwise rotation. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6873 | * |
| 6874 | * @param rotation The degrees of rotation. |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6875 | * |
| 6876 | * @see #getRotation() |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6877 | * @see #getPivotX() |
| 6878 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6879 | * @see #setRotationX(float) |
| 6880 | * @see #setRotationY(float) |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 6881 | * |
| 6882 | * @attr ref android.R.styleable#View_rotation |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6883 | */ |
| 6884 | public void setRotation(float rotation) { |
| 6885 | if (mRotation != rotation) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6886 | invalidateParentCaches(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6887 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6888 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6889 | mRotation = rotation; |
| 6890 | mMatrixDirty = true; |
| 6891 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6892 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6893 | } |
| 6894 | } |
| 6895 | |
| 6896 | /** |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6897 | * The degrees that the view is rotated around the vertical axis through the pivot point. |
| 6898 | * |
| 6899 | * @see #getPivotX() |
| 6900 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6901 | * @see #setRotationY(float) |
| 6902 | * |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6903 | * @return The degrees of Y rotation. |
| 6904 | */ |
| 6905 | public float getRotationY() { |
| 6906 | return mRotationY; |
| 6907 | } |
| 6908 | |
| 6909 | /** |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6910 | * Sets the degrees that the view is rotated around the vertical axis through the pivot point. |
| 6911 | * Increasing values result in counter-clockwise rotation from the viewpoint of looking |
| 6912 | * down the y axis. |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6913 | * |
| 6914 | * When rotating large views, it is recommended to adjust the camera distance |
| 6915 | * accordingly. Refer to {@link #setCameraDistance(float)} for more information. |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6916 | * |
| 6917 | * @param rotationY The degrees of Y rotation. |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6918 | * |
| 6919 | * @see #getRotationY() |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6920 | * @see #getPivotX() |
| 6921 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6922 | * @see #setRotation(float) |
| 6923 | * @see #setRotationX(float) |
| 6924 | * @see #setCameraDistance(float) |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 6925 | * |
| 6926 | * @attr ref android.R.styleable#View_rotationY |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6927 | */ |
| 6928 | public void setRotationY(float rotationY) { |
| 6929 | if (mRotationY != rotationY) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6930 | invalidateParentCaches(); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6931 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6932 | invalidate(false); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6933 | mRotationY = rotationY; |
| 6934 | mMatrixDirty = true; |
| 6935 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6936 | invalidate(false); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6937 | } |
| 6938 | } |
| 6939 | |
| 6940 | /** |
| 6941 | * The degrees that the view is rotated around the horizontal axis through the pivot point. |
| 6942 | * |
| 6943 | * @see #getPivotX() |
| 6944 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6945 | * @see #setRotationX(float) |
| 6946 | * |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6947 | * @return The degrees of X rotation. |
| 6948 | */ |
| 6949 | public float getRotationX() { |
| 6950 | return mRotationX; |
| 6951 | } |
| 6952 | |
| 6953 | /** |
Chet Haase | 897247b | 2010-09-09 14:54:47 -0700 | [diff] [blame] | 6954 | * Sets the degrees that the view is rotated around the horizontal axis through the pivot point. |
| 6955 | * Increasing values result in clockwise rotation from the viewpoint of looking down the |
| 6956 | * x axis. |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6957 | * |
| 6958 | * When rotating large views, it is recommended to adjust the camera distance |
| 6959 | * accordingly. Refer to {@link #setCameraDistance(float)} for more information. |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6960 | * |
| 6961 | * @param rotationX The degrees of X rotation. |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6962 | * |
| 6963 | * @see #getRotationX() |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6964 | * @see #getPivotX() |
| 6965 | * @see #getPivotY() |
Romain Guy | a5364ee | 2011-02-24 14:46:04 -0800 | [diff] [blame] | 6966 | * @see #setRotation(float) |
| 6967 | * @see #setRotationY(float) |
| 6968 | * @see #setCameraDistance(float) |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 6969 | * |
| 6970 | * @attr ref android.R.styleable#View_rotationX |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6971 | */ |
| 6972 | public void setRotationX(float rotationX) { |
| 6973 | if (mRotationX != rotationX) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 6974 | invalidateParentCaches(); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6975 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6976 | invalidate(false); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6977 | mRotationX = rotationX; |
| 6978 | mMatrixDirty = true; |
| 6979 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 6980 | invalidate(false); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | |
| 6984 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6985 | * The amount that the view is scaled in x around the pivot point, as a proportion of |
| 6986 | * the view's unscaled width. A value of 1, the default, means that no scaling is applied. |
| 6987 | * |
Joe Onorato | 9316232 | 2010-09-16 15:42:01 -0400 | [diff] [blame] | 6988 | * <p>By default, this is 1.0f. |
| 6989 | * |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 6990 | * @see #getPivotX() |
| 6991 | * @see #getPivotY() |
| 6992 | * @return The scaling factor. |
| 6993 | */ |
| 6994 | public float getScaleX() { |
| 6995 | return mScaleX; |
| 6996 | } |
| 6997 | |
| 6998 | /** |
| 6999 | * Sets the amount that the view is scaled in x around the pivot point, as a proportion of |
| 7000 | * the view's unscaled width. A value of 1 means that no scaling is applied. |
| 7001 | * |
| 7002 | * @param scaleX The scaling factor. |
| 7003 | * @see #getPivotX() |
| 7004 | * @see #getPivotY() |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7005 | * |
| 7006 | * @attr ref android.R.styleable#View_scaleX |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7007 | */ |
| 7008 | public void setScaleX(float scaleX) { |
| 7009 | if (mScaleX != scaleX) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7010 | invalidateParentCaches(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7011 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7012 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7013 | mScaleX = scaleX; |
| 7014 | mMatrixDirty = true; |
| 7015 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7016 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7017 | } |
| 7018 | } |
| 7019 | |
| 7020 | /** |
| 7021 | * The amount that the view is scaled in y around the pivot point, as a proportion of |
| 7022 | * the view's unscaled height. A value of 1, the default, means that no scaling is applied. |
| 7023 | * |
Joe Onorato | 9316232 | 2010-09-16 15:42:01 -0400 | [diff] [blame] | 7024 | * <p>By default, this is 1.0f. |
| 7025 | * |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7026 | * @see #getPivotX() |
| 7027 | * @see #getPivotY() |
| 7028 | * @return The scaling factor. |
| 7029 | */ |
| 7030 | public float getScaleY() { |
| 7031 | return mScaleY; |
| 7032 | } |
| 7033 | |
| 7034 | /** |
| 7035 | * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of |
| 7036 | * the view's unscaled width. A value of 1 means that no scaling is applied. |
| 7037 | * |
| 7038 | * @param scaleY The scaling factor. |
| 7039 | * @see #getPivotX() |
| 7040 | * @see #getPivotY() |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7041 | * |
| 7042 | * @attr ref android.R.styleable#View_scaleY |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7043 | */ |
| 7044 | public void setScaleY(float scaleY) { |
| 7045 | if (mScaleY != scaleY) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7046 | invalidateParentCaches(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7047 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7048 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7049 | mScaleY = scaleY; |
| 7050 | mMatrixDirty = true; |
| 7051 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7052 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | /** |
| 7057 | * The x location of the point around which the view is {@link #setRotation(float) rotated} |
| 7058 | * and {@link #setScaleX(float) scaled}. |
| 7059 | * |
| 7060 | * @see #getRotation() |
| 7061 | * @see #getScaleX() |
| 7062 | * @see #getScaleY() |
| 7063 | * @see #getPivotY() |
| 7064 | * @return The x location of the pivot point. |
| 7065 | */ |
| 7066 | public float getPivotX() { |
| 7067 | return mPivotX; |
| 7068 | } |
| 7069 | |
| 7070 | /** |
| 7071 | * Sets the x location of the point around which the view is |
| 7072 | * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}. |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 7073 | * By default, the pivot point is centered on the object. |
| 7074 | * Setting this property disables this behavior and causes the view to use only the |
| 7075 | * explicitly set pivotX and pivotY values. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7076 | * |
| 7077 | * @param pivotX The x location of the pivot point. |
| 7078 | * @see #getRotation() |
| 7079 | * @see #getScaleX() |
| 7080 | * @see #getScaleY() |
| 7081 | * @see #getPivotY() |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7082 | * |
| 7083 | * @attr ref android.R.styleable#View_transformPivotX |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7084 | */ |
| 7085 | public void setPivotX(float pivotX) { |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 7086 | mPrivateFlags |= PIVOT_EXPLICITLY_SET; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7087 | if (mPivotX != pivotX) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7088 | invalidateParentCaches(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7089 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7090 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7091 | mPivotX = pivotX; |
| 7092 | mMatrixDirty = true; |
| 7093 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7094 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7095 | } |
| 7096 | } |
| 7097 | |
| 7098 | /** |
| 7099 | * The y location of the point around which the view is {@link #setRotation(float) rotated} |
| 7100 | * and {@link #setScaleY(float) scaled}. |
| 7101 | * |
| 7102 | * @see #getRotation() |
| 7103 | * @see #getScaleX() |
| 7104 | * @see #getScaleY() |
| 7105 | * @see #getPivotY() |
| 7106 | * @return The y location of the pivot point. |
| 7107 | */ |
| 7108 | public float getPivotY() { |
| 7109 | return mPivotY; |
| 7110 | } |
| 7111 | |
| 7112 | /** |
| 7113 | * Sets the y location of the point around which the view is {@link #setRotation(float) rotated} |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 7114 | * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object. |
| 7115 | * Setting this property disables this behavior and causes the view to use only the |
| 7116 | * explicitly set pivotX and pivotY values. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7117 | * |
| 7118 | * @param pivotY The y location of the pivot point. |
| 7119 | * @see #getRotation() |
| 7120 | * @see #getScaleX() |
| 7121 | * @see #getScaleY() |
| 7122 | * @see #getPivotY() |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7123 | * |
| 7124 | * @attr ref android.R.styleable#View_transformPivotY |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7125 | */ |
| 7126 | public void setPivotY(float pivotY) { |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 7127 | mPrivateFlags |= PIVOT_EXPLICITLY_SET; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7128 | if (mPivotY != pivotY) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7129 | invalidateParentCaches(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7130 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7131 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7132 | mPivotY = pivotY; |
| 7133 | mMatrixDirty = true; |
| 7134 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7135 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7136 | } |
| 7137 | } |
| 7138 | |
| 7139 | /** |
| 7140 | * The opacity of the view. This is a value from 0 to 1, where 0 means the view is |
| 7141 | * completely transparent and 1 means the view is completely opaque. |
| 7142 | * |
Joe Onorato | 9316232 | 2010-09-16 15:42:01 -0400 | [diff] [blame] | 7143 | * <p>By default this is 1.0f. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7144 | * @return The opacity of the view. |
| 7145 | */ |
| 7146 | public float getAlpha() { |
| 7147 | return mAlpha; |
| 7148 | } |
| 7149 | |
| 7150 | /** |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 7151 | * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is |
| 7152 | * completely transparent and 1 means the view is completely opaque.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7153 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 7154 | * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is |
| 7155 | * responsible for applying the opacity itself. Otherwise, calling this method is |
| 7156 | * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7157 | * setting a hardware layer.</p> |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7158 | * |
| 7159 | * @param alpha The opacity of the view. |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7160 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7161 | * @see #setLayerType(int, android.graphics.Paint) |
| 7162 | * |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7163 | * @attr ref android.R.styleable#View_alpha |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7164 | */ |
| 7165 | public void setAlpha(float alpha) { |
| 7166 | mAlpha = alpha; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7167 | invalidateParentCaches(); |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7168 | if (onSetAlpha((int) (alpha * 255))) { |
Romain Guy | a3496a9 | 2010-10-12 11:53:24 -0700 | [diff] [blame] | 7169 | mPrivateFlags |= ALPHA_SET; |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7170 | // subclass is handling alpha - don't optimize rendering cache invalidation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7171 | invalidate(true); |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7172 | } else { |
Romain Guy | a3496a9 | 2010-10-12 11:53:24 -0700 | [diff] [blame] | 7173 | mPrivateFlags &= ~ALPHA_SET; |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7174 | invalidate(false); |
| 7175 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 7179 | * Faster version of setAlpha() which performs the same steps except there are |
| 7180 | * no calls to invalidate(). The caller of this function should perform proper invalidation |
| 7181 | * on the parent and this object. The return value indicates whether the subclass handles |
| 7182 | * alpha (the return value for onSetAlpha()). |
| 7183 | * |
| 7184 | * @param alpha The new value for the alpha property |
| 7185 | * @return true if the View subclass handles alpha (the return value for onSetAlpha()) |
| 7186 | */ |
| 7187 | boolean setAlphaNoInvalidation(float alpha) { |
| 7188 | mAlpha = alpha; |
| 7189 | boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255)); |
| 7190 | if (subclassHandlesAlpha) { |
| 7191 | mPrivateFlags |= ALPHA_SET; |
| 7192 | } else { |
| 7193 | mPrivateFlags &= ~ALPHA_SET; |
| 7194 | } |
| 7195 | return subclassHandlesAlpha; |
| 7196 | } |
| 7197 | |
| 7198 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7199 | * Top position of this view relative to its parent. |
| 7200 | * |
| 7201 | * @return The top of this view, in pixels. |
| 7202 | */ |
| 7203 | @ViewDebug.CapturedViewProperty |
| 7204 | public final int getTop() { |
| 7205 | return mTop; |
| 7206 | } |
| 7207 | |
| 7208 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7209 | * Sets the top position of this view relative to its parent. This method is meant to be called |
| 7210 | * by the layout system and should not generally be called otherwise, because the property |
| 7211 | * may be changed at any time by the layout. |
| 7212 | * |
| 7213 | * @param top The top of this view, in pixels. |
| 7214 | */ |
| 7215 | public final void setTop(int top) { |
| 7216 | if (top != mTop) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7217 | updateMatrix(); |
| 7218 | if (mMatrixIsIdentity) { |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7219 | if (mAttachInfo != null) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7220 | int minTop; |
| 7221 | int yLoc; |
| 7222 | if (top < mTop) { |
| 7223 | minTop = top; |
| 7224 | yLoc = top - mTop; |
| 7225 | } else { |
| 7226 | minTop = mTop; |
| 7227 | yLoc = 0; |
| 7228 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7229 | invalidate(0, yLoc, mRight - mLeft, mBottom - minTop); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7230 | } |
| 7231 | } else { |
| 7232 | // Double-invalidation is necessary to capture view's old and new areas |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7233 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7234 | } |
| 7235 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7236 | int width = mRight - mLeft; |
| 7237 | int oldHeight = mBottom - mTop; |
| 7238 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7239 | mTop = top; |
| 7240 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7241 | onSizeChanged(width, mBottom - mTop, width, oldHeight); |
| 7242 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7243 | if (!mMatrixIsIdentity) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 7244 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
| 7245 | // A change in dimension means an auto-centered pivot point changes, too |
| 7246 | mMatrixDirty = true; |
| 7247 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7248 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7249 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7250 | } |
Chet Haase | 55dbb65 | 2010-12-21 20:15:08 -0800 | [diff] [blame] | 7251 | mBackgroundSizeChanged = true; |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7252 | invalidateParentIfNeeded(); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7253 | } |
| 7254 | } |
| 7255 | |
| 7256 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7257 | * Bottom position of this view relative to its parent. |
| 7258 | * |
| 7259 | * @return The bottom of this view, in pixels. |
| 7260 | */ |
| 7261 | @ViewDebug.CapturedViewProperty |
| 7262 | public final int getBottom() { |
| 7263 | return mBottom; |
| 7264 | } |
| 7265 | |
| 7266 | /** |
Michael Jurka | dab559a | 2011-01-04 20:31:51 -0800 | [diff] [blame] | 7267 | * True if this view has changed since the last time being drawn. |
| 7268 | * |
| 7269 | * @return The dirty state of this view. |
| 7270 | */ |
| 7271 | public boolean isDirty() { |
| 7272 | return (mPrivateFlags & DIRTY_MASK) != 0; |
| 7273 | } |
| 7274 | |
| 7275 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7276 | * Sets the bottom position of this view relative to its parent. This method is meant to be |
| 7277 | * called by the layout system and should not generally be called otherwise, because the |
| 7278 | * property may be changed at any time by the layout. |
| 7279 | * |
| 7280 | * @param bottom The bottom of this view, in pixels. |
| 7281 | */ |
| 7282 | public final void setBottom(int bottom) { |
| 7283 | if (bottom != mBottom) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7284 | updateMatrix(); |
| 7285 | if (mMatrixIsIdentity) { |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7286 | if (mAttachInfo != null) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7287 | int maxBottom; |
| 7288 | if (bottom < mBottom) { |
| 7289 | maxBottom = mBottom; |
| 7290 | } else { |
| 7291 | maxBottom = bottom; |
| 7292 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7293 | invalidate(0, 0, mRight - mLeft, maxBottom - mTop); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7294 | } |
| 7295 | } else { |
| 7296 | // Double-invalidation is necessary to capture view's old and new areas |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7297 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7298 | } |
| 7299 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7300 | int width = mRight - mLeft; |
| 7301 | int oldHeight = mBottom - mTop; |
| 7302 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7303 | mBottom = bottom; |
| 7304 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7305 | onSizeChanged(width, mBottom - mTop, width, oldHeight); |
| 7306 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7307 | if (!mMatrixIsIdentity) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 7308 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
| 7309 | // A change in dimension means an auto-centered pivot point changes, too |
| 7310 | mMatrixDirty = true; |
| 7311 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7312 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7313 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7314 | } |
Chet Haase | 55dbb65 | 2010-12-21 20:15:08 -0800 | [diff] [blame] | 7315 | mBackgroundSizeChanged = true; |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7316 | invalidateParentIfNeeded(); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7317 | } |
| 7318 | } |
| 7319 | |
| 7320 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7321 | * Left position of this view relative to its parent. |
| 7322 | * |
| 7323 | * @return The left edge of this view, in pixels. |
| 7324 | */ |
| 7325 | @ViewDebug.CapturedViewProperty |
| 7326 | public final int getLeft() { |
| 7327 | return mLeft; |
| 7328 | } |
| 7329 | |
| 7330 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7331 | * Sets the left position of this view relative to its parent. This method is meant to be called |
| 7332 | * by the layout system and should not generally be called otherwise, because the property |
| 7333 | * may be changed at any time by the layout. |
| 7334 | * |
| 7335 | * @param left The bottom of this view, in pixels. |
| 7336 | */ |
| 7337 | public final void setLeft(int left) { |
| 7338 | if (left != mLeft) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7339 | updateMatrix(); |
| 7340 | if (mMatrixIsIdentity) { |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7341 | if (mAttachInfo != null) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7342 | int minLeft; |
| 7343 | int xLoc; |
| 7344 | if (left < mLeft) { |
| 7345 | minLeft = left; |
| 7346 | xLoc = left - mLeft; |
| 7347 | } else { |
| 7348 | minLeft = mLeft; |
| 7349 | xLoc = 0; |
| 7350 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7351 | invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7352 | } |
| 7353 | } else { |
| 7354 | // Double-invalidation is necessary to capture view's old and new areas |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7355 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7356 | } |
| 7357 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7358 | int oldWidth = mRight - mLeft; |
| 7359 | int height = mBottom - mTop; |
| 7360 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7361 | mLeft = left; |
| 7362 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7363 | onSizeChanged(mRight - mLeft, height, oldWidth, height); |
| 7364 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7365 | if (!mMatrixIsIdentity) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 7366 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
| 7367 | // A change in dimension means an auto-centered pivot point changes, too |
| 7368 | mMatrixDirty = true; |
| 7369 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7370 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7371 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7372 | } |
Chet Haase | 55dbb65 | 2010-12-21 20:15:08 -0800 | [diff] [blame] | 7373 | mBackgroundSizeChanged = true; |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7374 | invalidateParentIfNeeded(); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7375 | } |
| 7376 | } |
| 7377 | |
| 7378 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7379 | * Right position of this view relative to its parent. |
| 7380 | * |
| 7381 | * @return The right edge of this view, in pixels. |
| 7382 | */ |
| 7383 | @ViewDebug.CapturedViewProperty |
| 7384 | public final int getRight() { |
| 7385 | return mRight; |
| 7386 | } |
| 7387 | |
| 7388 | /** |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7389 | * Sets the right position of this view relative to its parent. This method is meant to be called |
| 7390 | * by the layout system and should not generally be called otherwise, because the property |
| 7391 | * may be changed at any time by the layout. |
| 7392 | * |
| 7393 | * @param right The bottom of this view, in pixels. |
| 7394 | */ |
| 7395 | public final void setRight(int right) { |
| 7396 | if (right != mRight) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7397 | updateMatrix(); |
| 7398 | if (mMatrixIsIdentity) { |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7399 | if (mAttachInfo != null) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7400 | int maxRight; |
| 7401 | if (right < mRight) { |
| 7402 | maxRight = mRight; |
| 7403 | } else { |
| 7404 | maxRight = right; |
| 7405 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 7406 | invalidate(0, 0, maxRight - mLeft, mBottom - mTop); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7407 | } |
| 7408 | } else { |
| 7409 | // Double-invalidation is necessary to capture view's old and new areas |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7410 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7411 | } |
| 7412 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7413 | int oldWidth = mRight - mLeft; |
| 7414 | int height = mBottom - mTop; |
| 7415 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7416 | mRight = right; |
| 7417 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7418 | onSizeChanged(mRight - mLeft, height, oldWidth, height); |
| 7419 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7420 | if (!mMatrixIsIdentity) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 7421 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
| 7422 | // A change in dimension means an auto-centered pivot point changes, too |
| 7423 | mMatrixDirty = true; |
| 7424 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7425 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7426 | invalidate(true); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7427 | } |
Chet Haase | 55dbb65 | 2010-12-21 20:15:08 -0800 | [diff] [blame] | 7428 | mBackgroundSizeChanged = true; |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7429 | invalidateParentIfNeeded(); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 7430 | } |
| 7431 | } |
| 7432 | |
| 7433 | /** |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7434 | * The visual x position of this view, in pixels. This is equivalent to the |
| 7435 | * {@link #setTranslationX(float) translationX} property plus the current |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7436 | * {@link #getLeft() left} property. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7437 | * |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7438 | * @return The visual x position of this view, in pixels. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7439 | */ |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7440 | public float getX() { |
| 7441 | return mLeft + mTranslationX; |
| 7442 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7443 | |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7444 | /** |
| 7445 | * Sets the visual x position of this view, in pixels. This is equivalent to setting the |
| 7446 | * {@link #setTranslationX(float) translationX} property to be the difference between |
| 7447 | * the x value passed in and the current {@link #getLeft() left} property. |
| 7448 | * |
| 7449 | * @param x The visual x position of this view, in pixels. |
| 7450 | */ |
| 7451 | public void setX(float x) { |
| 7452 | setTranslationX(x - mLeft); |
| 7453 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7454 | |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7455 | /** |
| 7456 | * The visual y position of this view, in pixels. This is equivalent to the |
| 7457 | * {@link #setTranslationY(float) translationY} property plus the current |
| 7458 | * {@link #getTop() top} property. |
| 7459 | * |
| 7460 | * @return The visual y position of this view, in pixels. |
| 7461 | */ |
| 7462 | public float getY() { |
| 7463 | return mTop + mTranslationY; |
| 7464 | } |
| 7465 | |
| 7466 | /** |
| 7467 | * Sets the visual y position of this view, in pixels. This is equivalent to setting the |
| 7468 | * {@link #setTranslationY(float) translationY} property to be the difference between |
| 7469 | * the y value passed in and the current {@link #getTop() top} property. |
| 7470 | * |
| 7471 | * @param y The visual y position of this view, in pixels. |
| 7472 | */ |
| 7473 | public void setY(float y) { |
| 7474 | setTranslationY(y - mTop); |
| 7475 | } |
| 7476 | |
| 7477 | |
| 7478 | /** |
| 7479 | * The horizontal location of this view relative to its {@link #getLeft() left} position. |
| 7480 | * This position is post-layout, in addition to wherever the object's |
| 7481 | * layout placed it. |
| 7482 | * |
| 7483 | * @return The horizontal position of this view relative to its left position, in pixels. |
| 7484 | */ |
| 7485 | public float getTranslationX() { |
| 7486 | return mTranslationX; |
| 7487 | } |
| 7488 | |
| 7489 | /** |
| 7490 | * Sets the horizontal location of this view relative to its {@link #getLeft() left} position. |
| 7491 | * This effectively positions the object post-layout, in addition to wherever the object's |
| 7492 | * layout placed it. |
| 7493 | * |
| 7494 | * @param translationX The horizontal position of this view relative to its left position, |
| 7495 | * in pixels. |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7496 | * |
| 7497 | * @attr ref android.R.styleable#View_translationX |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7498 | */ |
| 7499 | public void setTranslationX(float translationX) { |
| 7500 | if (mTranslationX != translationX) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7501 | invalidateParentCaches(); |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7502 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7503 | invalidate(false); |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7504 | mTranslationX = translationX; |
| 7505 | mMatrixDirty = true; |
| 7506 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7507 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7508 | } |
| 7509 | } |
| 7510 | |
| 7511 | /** |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7512 | * The horizontal location of this view relative to its {@link #getTop() top} position. |
| 7513 | * This position is post-layout, in addition to wherever the object's |
| 7514 | * layout placed it. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7515 | * |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7516 | * @return The vertical position of this view relative to its top position, |
| 7517 | * in pixels. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7518 | */ |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7519 | public float getTranslationY() { |
| 7520 | return mTranslationY; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7521 | } |
| 7522 | |
| 7523 | /** |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7524 | * Sets the vertical location of this view relative to its {@link #getTop() top} position. |
| 7525 | * This effectively positions the object post-layout, in addition to wherever the object's |
| 7526 | * layout placed it. |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7527 | * |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7528 | * @param translationY The vertical position of this view relative to its top position, |
| 7529 | * in pixels. |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 7530 | * |
| 7531 | * @attr ref android.R.styleable#View_translationY |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7532 | */ |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7533 | public void setTranslationY(float translationY) { |
| 7534 | if (mTranslationY != translationY) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7535 | invalidateParentCaches(); |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7536 | // Double-invalidation is necessary to capture view's old and new areas |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7537 | invalidate(false); |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7538 | mTranslationY = translationY; |
| 7539 | mMatrixDirty = true; |
| 7540 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7541 | invalidate(false); |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 7542 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | /** |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 7546 | * @hide |
| 7547 | */ |
Michael Jurka | dece29f | 2011-02-03 01:41:49 -0800 | [diff] [blame] | 7548 | public void setFastTranslationX(float x) { |
| 7549 | mTranslationX = x; |
| 7550 | mMatrixDirty = true; |
| 7551 | } |
| 7552 | |
| 7553 | /** |
| 7554 | * @hide |
| 7555 | */ |
| 7556 | public void setFastTranslationY(float y) { |
| 7557 | mTranslationY = y; |
| 7558 | mMatrixDirty = true; |
| 7559 | } |
| 7560 | |
| 7561 | /** |
| 7562 | * @hide |
| 7563 | */ |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 7564 | public void setFastX(float x) { |
| 7565 | mTranslationX = x - mLeft; |
| 7566 | mMatrixDirty = true; |
| 7567 | } |
| 7568 | |
| 7569 | /** |
| 7570 | * @hide |
| 7571 | */ |
| 7572 | public void setFastY(float y) { |
| 7573 | mTranslationY = y - mTop; |
| 7574 | mMatrixDirty = true; |
| 7575 | } |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 7576 | |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 7577 | /** |
| 7578 | * @hide |
| 7579 | */ |
| 7580 | public void setFastScaleX(float x) { |
| 7581 | mScaleX = x; |
| 7582 | mMatrixDirty = true; |
| 7583 | } |
| 7584 | |
| 7585 | /** |
| 7586 | * @hide |
| 7587 | */ |
| 7588 | public void setFastScaleY(float y) { |
| 7589 | mScaleY = y; |
| 7590 | mMatrixDirty = true; |
| 7591 | } |
| 7592 | |
| 7593 | /** |
| 7594 | * @hide |
| 7595 | */ |
| 7596 | public void setFastAlpha(float alpha) { |
| 7597 | mAlpha = alpha; |
| 7598 | } |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 7599 | |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 7600 | /** |
| 7601 | * @hide |
| 7602 | */ |
| 7603 | public void setFastRotationY(float y) { |
| 7604 | mRotationY = y; |
| 7605 | mMatrixDirty = true; |
| 7606 | } |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 7607 | |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 7608 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7609 | * Hit rectangle in parent's coordinates |
| 7610 | * |
| 7611 | * @param outRect The hit rectangle of the view. |
| 7612 | */ |
| 7613 | public void getHitRect(Rect outRect) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7614 | updateMatrix(); |
| 7615 | if (mMatrixIsIdentity || mAttachInfo == null) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7616 | outRect.set(mLeft, mTop, mRight, mBottom); |
| 7617 | } else { |
| 7618 | final RectF tmpRect = mAttachInfo.mTmpTransformRect; |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7619 | tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY); |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7620 | mMatrix.mapRect(tmpRect); |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7621 | outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop, |
| 7622 | (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | /** |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7627 | * Determines whether the given point, in local coordinates is inside the view. |
| 7628 | */ |
| 7629 | /*package*/ final boolean pointInView(float localX, float localY) { |
| 7630 | return localX >= 0 && localX < (mRight - mLeft) |
| 7631 | && localY >= 0 && localY < (mBottom - mTop); |
| 7632 | } |
| 7633 | |
| 7634 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7635 | * Utility method to determine whether the given point, in local coordinates, |
| 7636 | * is inside the view, where the area of the view is expanded by the slop factor. |
| 7637 | * This method is called while processing touch-move events to determine if the event |
| 7638 | * is still within the view. |
| 7639 | */ |
| 7640 | private boolean pointInView(float localX, float localY, float slop) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7641 | return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) && |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7642 | localY < ((mBottom - mTop) + slop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7643 | } |
| 7644 | |
| 7645 | /** |
| 7646 | * When a view has focus and the user navigates away from it, the next view is searched for |
| 7647 | * starting from the rectangle filled in by this method. |
| 7648 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 7649 | * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)}) |
| 7650 | * of the view. However, if your view maintains some idea of internal selection, |
| 7651 | * such as a cursor, or a selected row or column, you should override this method and |
| 7652 | * fill in a more specific rectangle. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7653 | * |
| 7654 | * @param r The rectangle to fill in, in this view's coordinates. |
| 7655 | */ |
| 7656 | public void getFocusedRect(Rect r) { |
| 7657 | getDrawingRect(r); |
| 7658 | } |
| 7659 | |
| 7660 | /** |
| 7661 | * If some part of this view is not clipped by any of its parents, then |
| 7662 | * return that area in r in global (root) coordinates. To convert r to local |
| 7663 | * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x, |
| 7664 | * -globalOffset.y)) If the view is completely clipped or translated out, |
| 7665 | * return false. |
| 7666 | * |
| 7667 | * @param r If true is returned, r holds the global coordinates of the |
| 7668 | * visible portion of this view. |
| 7669 | * @param globalOffset If true is returned, globalOffset holds the dx,dy |
| 7670 | * between this view and its root. globalOffet may be null. |
| 7671 | * @return true if r is non-empty (i.e. part of the view is visible at the |
| 7672 | * root level. |
| 7673 | */ |
| 7674 | public boolean getGlobalVisibleRect(Rect r, Point globalOffset) { |
| 7675 | int width = mRight - mLeft; |
| 7676 | int height = mBottom - mTop; |
| 7677 | if (width > 0 && height > 0) { |
| 7678 | r.set(0, 0, width, height); |
| 7679 | if (globalOffset != null) { |
| 7680 | globalOffset.set(-mScrollX, -mScrollY); |
| 7681 | } |
| 7682 | return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset); |
| 7683 | } |
| 7684 | return false; |
| 7685 | } |
| 7686 | |
| 7687 | public final boolean getGlobalVisibleRect(Rect r) { |
| 7688 | return getGlobalVisibleRect(r, null); |
| 7689 | } |
| 7690 | |
| 7691 | public final boolean getLocalVisibleRect(Rect r) { |
| 7692 | Point offset = new Point(); |
| 7693 | if (getGlobalVisibleRect(r, offset)) { |
| 7694 | r.offset(-offset.x, -offset.y); // make r local |
| 7695 | return true; |
| 7696 | } |
| 7697 | return false; |
| 7698 | } |
| 7699 | |
| 7700 | /** |
| 7701 | * Offset this view's vertical location by the specified number of pixels. |
| 7702 | * |
| 7703 | * @param offset the number of pixels to offset the view by |
| 7704 | */ |
| 7705 | public void offsetTopAndBottom(int offset) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7706 | if (offset != 0) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7707 | updateMatrix(); |
| 7708 | if (mMatrixIsIdentity) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7709 | final ViewParent p = mParent; |
| 7710 | if (p != null && mAttachInfo != null) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7711 | final Rect r = mAttachInfo.mTmpInvalRect; |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7712 | int minTop; |
| 7713 | int maxBottom; |
| 7714 | int yLoc; |
| 7715 | if (offset < 0) { |
| 7716 | minTop = mTop + offset; |
| 7717 | maxBottom = mBottom; |
| 7718 | yLoc = offset; |
| 7719 | } else { |
| 7720 | minTop = mTop; |
| 7721 | maxBottom = mBottom + offset; |
| 7722 | yLoc = 0; |
| 7723 | } |
| 7724 | r.set(0, yLoc, mRight - mLeft, maxBottom - minTop); |
| 7725 | p.invalidateChild(this, r); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7726 | } |
| 7727 | } else { |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7728 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7729 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7730 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7731 | mTop += offset; |
| 7732 | mBottom += offset; |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7733 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7734 | if (!mMatrixIsIdentity) { |
| 7735 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7736 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7737 | } |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7738 | invalidateParentIfNeeded(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7740 | } |
| 7741 | |
| 7742 | /** |
| 7743 | * Offset this view's horizontal location by the specified amount of pixels. |
| 7744 | * |
| 7745 | * @param offset the numer of pixels to offset the view by |
| 7746 | */ |
| 7747 | public void offsetLeftAndRight(int offset) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7748 | if (offset != 0) { |
Jeff Brown | 8667174 | 2010-09-30 20:00:15 -0700 | [diff] [blame] | 7749 | updateMatrix(); |
| 7750 | if (mMatrixIsIdentity) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7751 | final ViewParent p = mParent; |
| 7752 | if (p != null && mAttachInfo != null) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7753 | final Rect r = mAttachInfo.mTmpInvalRect; |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7754 | int minLeft; |
| 7755 | int maxRight; |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7756 | if (offset < 0) { |
| 7757 | minLeft = mLeft + offset; |
| 7758 | maxRight = mRight; |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7759 | } else { |
| 7760 | minLeft = mLeft; |
| 7761 | maxRight = mRight + offset; |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7762 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7763 | r.set(0, 0, maxRight - minLeft, mBottom - mTop); |
Chet Haase | 8fbf8d2 | 2010-07-30 15:01:32 -0700 | [diff] [blame] | 7764 | p.invalidateChild(this, r); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7765 | } |
| 7766 | } else { |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7767 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7768 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7769 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7770 | mLeft += offset; |
| 7771 | mRight += offset; |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 7772 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7773 | if (!mMatrixIsIdentity) { |
| 7774 | mPrivateFlags |= DRAWN; // force another invalidation with the new orientation |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 7775 | invalidate(false); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7776 | } |
Chet Haase | 678e0ad | 2011-01-25 09:37:18 -0800 | [diff] [blame] | 7777 | invalidateParentIfNeeded(); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 7778 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | /** |
| 7782 | * Get the LayoutParams associated with this view. All views should have |
| 7783 | * layout parameters. These supply parameters to the <i>parent</i> of this |
| 7784 | * view specifying how it should be arranged. There are many subclasses of |
| 7785 | * ViewGroup.LayoutParams, and these correspond to the different subclasses |
| 7786 | * of ViewGroup that are responsible for arranging their children. |
Romain Guy | 01c174b | 2011-02-22 11:51:06 -0800 | [diff] [blame] | 7787 | * |
| 7788 | * This method may return null if this View is not attached to a parent |
| 7789 | * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)} |
| 7790 | * was not invoked successfully. When a View is attached to a parent |
| 7791 | * ViewGroup, this method must not return null. |
| 7792 | * |
| 7793 | * @return The LayoutParams associated with this view, or null if no |
| 7794 | * parameters have been set yet |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7795 | */ |
Konstantin Lopyrev | 91a7f5f | 2010-08-10 18:54:54 -0700 | [diff] [blame] | 7796 | @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7797 | public ViewGroup.LayoutParams getLayoutParams() { |
| 7798 | return mLayoutParams; |
| 7799 | } |
| 7800 | |
| 7801 | /** |
| 7802 | * Set the layout parameters associated with this view. These supply |
| 7803 | * parameters to the <i>parent</i> of this view specifying how it should be |
| 7804 | * arranged. There are many subclasses of ViewGroup.LayoutParams, and these |
| 7805 | * correspond to the different subclasses of ViewGroup that are responsible |
| 7806 | * for arranging their children. |
| 7807 | * |
Romain Guy | 01c174b | 2011-02-22 11:51:06 -0800 | [diff] [blame] | 7808 | * @param params The layout parameters for this view, cannot be null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7809 | */ |
| 7810 | public void setLayoutParams(ViewGroup.LayoutParams params) { |
| 7811 | if (params == null) { |
Romain Guy | 01c174b | 2011-02-22 11:51:06 -0800 | [diff] [blame] | 7812 | throw new NullPointerException("Layout parameters cannot be null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7813 | } |
| 7814 | mLayoutParams = params; |
| 7815 | requestLayout(); |
| 7816 | } |
| 7817 | |
| 7818 | /** |
| 7819 | * Set the scrolled position of your view. This will cause a call to |
| 7820 | * {@link #onScrollChanged(int, int, int, int)} and the view will be |
| 7821 | * invalidated. |
| 7822 | * @param x the x position to scroll to |
| 7823 | * @param y the y position to scroll to |
| 7824 | */ |
| 7825 | public void scrollTo(int x, int y) { |
| 7826 | if (mScrollX != x || mScrollY != y) { |
| 7827 | int oldX = mScrollX; |
| 7828 | int oldY = mScrollY; |
| 7829 | mScrollX = x; |
| 7830 | mScrollY = y; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7831 | invalidateParentCaches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7832 | onScrollChanged(mScrollX, mScrollY, oldX, oldY); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7833 | if (!awakenScrollBars()) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7834 | invalidate(true); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7835 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | /** |
| 7840 | * Move the scrolled position of your view. This will cause a call to |
| 7841 | * {@link #onScrollChanged(int, int, int, int)} and the view will be |
| 7842 | * invalidated. |
| 7843 | * @param x the amount of pixels to scroll by horizontally |
| 7844 | * @param y the amount of pixels to scroll by vertically |
| 7845 | */ |
| 7846 | public void scrollBy(int x, int y) { |
| 7847 | scrollTo(mScrollX + x, mScrollY + y); |
| 7848 | } |
| 7849 | |
| 7850 | /** |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7851 | * <p>Trigger the scrollbars to draw. When invoked this method starts an |
| 7852 | * animation to fade the scrollbars out after a default delay. If a subclass |
| 7853 | * provides animated scrolling, the start delay should equal the duration |
| 7854 | * of the scrolling animation.</p> |
| 7855 | * |
| 7856 | * <p>The animation starts only if at least one of the scrollbars is |
| 7857 | * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and |
| 7858 | * {@link #isVerticalScrollBarEnabled()}. When the animation is started, |
| 7859 | * this method returns true, and false otherwise. If the animation is |
| 7860 | * started, this method calls {@link #invalidate()}; in that case the |
| 7861 | * caller should not call {@link #invalidate()}.</p> |
| 7862 | * |
| 7863 | * <p>This method should be invoked every time a subclass directly updates |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 7864 | * the scroll parameters.</p> |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7865 | * |
| 7866 | * <p>This method is automatically invoked by {@link #scrollBy(int, int)} |
| 7867 | * and {@link #scrollTo(int, int)}.</p> |
| 7868 | * |
| 7869 | * @return true if the animation is played, false otherwise |
| 7870 | * |
| 7871 | * @see #awakenScrollBars(int) |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7872 | * @see #scrollBy(int, int) |
| 7873 | * @see #scrollTo(int, int) |
| 7874 | * @see #isHorizontalScrollBarEnabled() |
| 7875 | * @see #isVerticalScrollBarEnabled() |
| 7876 | * @see #setHorizontalScrollBarEnabled(boolean) |
| 7877 | * @see #setVerticalScrollBarEnabled(boolean) |
| 7878 | */ |
| 7879 | protected boolean awakenScrollBars() { |
| 7880 | return mScrollCache != null && |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7881 | awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7882 | } |
| 7883 | |
| 7884 | /** |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 7885 | * Trigger the scrollbars to draw. |
| 7886 | * This method differs from awakenScrollBars() only in its default duration. |
| 7887 | * initialAwakenScrollBars() will show the scroll bars for longer than |
| 7888 | * usual to give the user more of a chance to notice them. |
| 7889 | * |
| 7890 | * @return true if the animation is played, false otherwise. |
| 7891 | */ |
| 7892 | private boolean initialAwakenScrollBars() { |
| 7893 | return mScrollCache != null && |
| 7894 | awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true); |
| 7895 | } |
| 7896 | |
| 7897 | /** |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7898 | * <p> |
| 7899 | * Trigger the scrollbars to draw. When invoked this method starts an |
| 7900 | * animation to fade the scrollbars out after a fixed delay. If a subclass |
| 7901 | * provides animated scrolling, the start delay should equal the duration of |
| 7902 | * the scrolling animation. |
| 7903 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7904 | * |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7905 | * <p> |
| 7906 | * The animation starts only if at least one of the scrollbars is enabled, |
| 7907 | * as specified by {@link #isHorizontalScrollBarEnabled()} and |
| 7908 | * {@link #isVerticalScrollBarEnabled()}. When the animation is started, |
| 7909 | * this method returns true, and false otherwise. If the animation is |
| 7910 | * started, this method calls {@link #invalidate()}; in that case the caller |
| 7911 | * should not call {@link #invalidate()}. |
| 7912 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7913 | * |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7914 | * <p> |
| 7915 | * This method should be invoked everytime a subclass directly updates the |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 7916 | * scroll parameters. |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7917 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7918 | * |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7919 | * @param startDelay the delay, in milliseconds, after which the animation |
| 7920 | * should start; when the delay is 0, the animation starts |
| 7921 | * immediately |
| 7922 | * @return true if the animation is played, false otherwise |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7923 | * |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7924 | * @see #scrollBy(int, int) |
| 7925 | * @see #scrollTo(int, int) |
| 7926 | * @see #isHorizontalScrollBarEnabled() |
| 7927 | * @see #isVerticalScrollBarEnabled() |
| 7928 | * @see #setHorizontalScrollBarEnabled(boolean) |
| 7929 | * @see #setVerticalScrollBarEnabled(boolean) |
| 7930 | */ |
| 7931 | protected boolean awakenScrollBars(int startDelay) { |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7932 | return awakenScrollBars(startDelay, true); |
| 7933 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7934 | |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7935 | /** |
| 7936 | * <p> |
| 7937 | * Trigger the scrollbars to draw. When invoked this method starts an |
| 7938 | * animation to fade the scrollbars out after a fixed delay. If a subclass |
| 7939 | * provides animated scrolling, the start delay should equal the duration of |
| 7940 | * the scrolling animation. |
| 7941 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7942 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7943 | * <p> |
| 7944 | * The animation starts only if at least one of the scrollbars is enabled, |
| 7945 | * as specified by {@link #isHorizontalScrollBarEnabled()} and |
| 7946 | * {@link #isVerticalScrollBarEnabled()}. When the animation is started, |
| 7947 | * this method returns true, and false otherwise. If the animation is |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7948 | * started, this method calls {@link #invalidate()} if the invalidate parameter |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7949 | * is set to true; in that case the caller |
| 7950 | * should not call {@link #invalidate()}. |
| 7951 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7952 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7953 | * <p> |
| 7954 | * This method should be invoked everytime a subclass directly updates the |
| 7955 | * scroll parameters. |
| 7956 | * </p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7957 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7958 | * @param startDelay the delay, in milliseconds, after which the animation |
| 7959 | * should start; when the delay is 0, the animation starts |
| 7960 | * immediately |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7961 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7962 | * @param invalidate Wheter this method should call invalidate |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7963 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7964 | * @return true if the animation is played, false otherwise |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7965 | * |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7966 | * @see #scrollBy(int, int) |
| 7967 | * @see #scrollTo(int, int) |
| 7968 | * @see #isHorizontalScrollBarEnabled() |
| 7969 | * @see #isVerticalScrollBarEnabled() |
| 7970 | * @see #setHorizontalScrollBarEnabled(boolean) |
| 7971 | * @see #setVerticalScrollBarEnabled(boolean) |
| 7972 | */ |
| 7973 | protected boolean awakenScrollBars(int startDelay, boolean invalidate) { |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7974 | final ScrollabilityCache scrollCache = mScrollCache; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 7975 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7976 | if (scrollCache == null || !scrollCache.fadeScrollBars) { |
| 7977 | return false; |
| 7978 | } |
| 7979 | |
| 7980 | if (scrollCache.scrollBar == null) { |
| 7981 | scrollCache.scrollBar = new ScrollBarDrawable(); |
| 7982 | } |
| 7983 | |
| 7984 | if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) { |
| 7985 | |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7986 | if (invalidate) { |
| 7987 | // Invalidate to show the scrollbars |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 7988 | invalidate(true); |
Mike Cleron | 290947b | 2009-09-29 18:34:32 -0700 | [diff] [blame] | 7989 | } |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 7990 | |
| 7991 | if (scrollCache.state == ScrollabilityCache.OFF) { |
| 7992 | // FIXME: this is copied from WindowManagerService. |
| 7993 | // We should get this value from the system when it |
| 7994 | // is possible to do so. |
| 7995 | final int KEY_REPEAT_FIRST_DELAY = 750; |
| 7996 | startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay); |
| 7997 | } |
| 7998 | |
| 7999 | // Tell mScrollCache when we should start fading. This may |
| 8000 | // extend the fade start time if one was already scheduled |
Mike Cleron | 3ecd58c | 2009-09-28 11:39:02 -0700 | [diff] [blame] | 8001 | long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay; |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8002 | scrollCache.fadeStartTime = fadeStartTime; |
| 8003 | scrollCache.state = ScrollabilityCache.ON; |
| 8004 | |
| 8005 | // Schedule our fader to run, unscheduling any old ones first |
| 8006 | if (mAttachInfo != null) { |
| 8007 | mAttachInfo.mHandler.removeCallbacks(scrollCache); |
| 8008 | mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime); |
| 8009 | } |
| 8010 | |
| 8011 | return true; |
| 8012 | } |
| 8013 | |
| 8014 | return false; |
| 8015 | } |
| 8016 | |
| 8017 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8018 | * Mark the the area defined by dirty as needing to be drawn. If the view is |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 8019 | * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point |
| 8020 | * in the future. This must be called from a UI thread. To call from a non-UI |
| 8021 | * thread, call {@link #postInvalidate()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8022 | * |
| 8023 | * WARNING: This method is destructive to dirty. |
| 8024 | * @param dirty the rectangle representing the bounds of the dirty region |
| 8025 | */ |
| 8026 | public void invalidate(Rect dirty) { |
| 8027 | if (ViewDebug.TRACE_HIERARCHY) { |
| 8028 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE); |
| 8029 | } |
| 8030 | |
Romain Guy | 2fe9a8f | 2010-10-04 20:17:01 -0700 | [diff] [blame] | 8031 | if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) || |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8032 | (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID || |
| 8033 | (mPrivateFlags & INVALIDATED) != INVALIDATED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8034 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8035 | mPrivateFlags |= INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8036 | final ViewParent p = mParent; |
| 8037 | final AttachInfo ai = mAttachInfo; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8038 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 8039 | if (!HardwareRenderer.RENDER_DIRTY_REGIONS) { |
| 8040 | if (p != null && ai != null && ai.mHardwareAccelerated) { |
| 8041 | // fast-track for GL-enabled applications; just invalidate the whole hierarchy |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 8042 | // with a null dirty rect, which tells the ViewAncestor to redraw everything |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8043 | p.invalidateChild(this, null); |
| 8044 | return; |
| 8045 | } |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 8046 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8047 | if (p != null && ai != null) { |
| 8048 | final int scrollX = mScrollX; |
| 8049 | final int scrollY = mScrollY; |
| 8050 | final Rect r = ai.mTmpInvalRect; |
| 8051 | r.set(dirty.left - scrollX, dirty.top - scrollY, |
| 8052 | dirty.right - scrollX, dirty.bottom - scrollY); |
| 8053 | mParent.invalidateChild(this, r); |
| 8054 | } |
| 8055 | } |
| 8056 | } |
| 8057 | |
| 8058 | /** |
| 8059 | * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn. |
| 8060 | * The coordinates of the dirty rect are relative to the view. |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 8061 | * If the view is visible, {@link #onDraw(android.graphics.Canvas)} |
| 8062 | * will be called at some point in the future. This must be called from |
| 8063 | * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8064 | * @param l the left position of the dirty region |
| 8065 | * @param t the top position of the dirty region |
| 8066 | * @param r the right position of the dirty region |
| 8067 | * @param b the bottom position of the dirty region |
| 8068 | */ |
| 8069 | public void invalidate(int l, int t, int r, int b) { |
| 8070 | if (ViewDebug.TRACE_HIERARCHY) { |
| 8071 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE); |
| 8072 | } |
| 8073 | |
Romain Guy | 2fe9a8f | 2010-10-04 20:17:01 -0700 | [diff] [blame] | 8074 | if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) || |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8075 | (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID || |
| 8076 | (mPrivateFlags & INVALIDATED) != INVALIDATED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8077 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8078 | mPrivateFlags |= INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8079 | final ViewParent p = mParent; |
| 8080 | final AttachInfo ai = mAttachInfo; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8081 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 8082 | if (!HardwareRenderer.RENDER_DIRTY_REGIONS) { |
| 8083 | if (p != null && ai != null && ai.mHardwareAccelerated) { |
| 8084 | // fast-track for GL-enabled applications; just invalidate the whole hierarchy |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 8085 | // with a null dirty rect, which tells the ViewAncestor to redraw everything |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8086 | p.invalidateChild(this, null); |
| 8087 | return; |
| 8088 | } |
Chet Haase | f2f7d8f | 2010-12-03 14:08:14 -0800 | [diff] [blame] | 8089 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8090 | if (p != null && ai != null && l < r && t < b) { |
| 8091 | final int scrollX = mScrollX; |
| 8092 | final int scrollY = mScrollY; |
| 8093 | final Rect tmpr = ai.mTmpInvalRect; |
| 8094 | tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY); |
| 8095 | p.invalidateChild(this, tmpr); |
| 8096 | } |
| 8097 | } |
| 8098 | } |
| 8099 | |
| 8100 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 8101 | * Invalidate the whole view. If the view is visible, |
| 8102 | * {@link #onDraw(android.graphics.Canvas)} will be called at some point in |
| 8103 | * the future. This must be called from a UI thread. To call from a non-UI thread, |
| 8104 | * call {@link #postInvalidate()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8105 | */ |
| 8106 | public void invalidate() { |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 8107 | invalidate(true); |
| 8108 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8109 | |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 8110 | /** |
| 8111 | * This is where the invalidate() work actually happens. A full invalidate() |
| 8112 | * causes the drawing cache to be invalidated, but this function can be called with |
| 8113 | * invalidateCache set to false to skip that invalidation step for cases that do not |
| 8114 | * need it (for example, a component that remains at the same dimensions with the same |
| 8115 | * content). |
| 8116 | * |
| 8117 | * @param invalidateCache Whether the drawing cache for this view should be invalidated as |
| 8118 | * well. This is usually true for a full invalidate, but may be set to false if the |
| 8119 | * View's contents or dimensions have not changed. |
| 8120 | */ |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 8121 | void invalidate(boolean invalidateCache) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8122 | if (ViewDebug.TRACE_HIERARCHY) { |
| 8123 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE); |
| 8124 | } |
| 8125 | |
Romain Guy | 2fe9a8f | 2010-10-04 20:17:01 -0700 | [diff] [blame] | 8126 | if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) || |
Romain Guy | c5d5586 | 2011-01-21 19:01:46 -0800 | [diff] [blame] | 8127 | (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) || |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8128 | (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) { |
| 8129 | mLastIsOpaque = isOpaque(); |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 8130 | mPrivateFlags &= ~DRAWN; |
| 8131 | if (invalidateCache) { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8132 | mPrivateFlags |= INVALIDATED; |
Chet Haase | ed03270 | 2010-10-01 14:05:54 -0700 | [diff] [blame] | 8133 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
| 8134 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8135 | final AttachInfo ai = mAttachInfo; |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 8136 | final ViewParent p = mParent; |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8137 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 8138 | if (!HardwareRenderer.RENDER_DIRTY_REGIONS) { |
| 8139 | if (p != null && ai != null && ai.mHardwareAccelerated) { |
| 8140 | // fast-track for GL-enabled applications; just invalidate the whole hierarchy |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 8141 | // with a null dirty rect, which tells the ViewAncestor to redraw everything |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 8142 | p.invalidateChild(this, null); |
| 8143 | return; |
| 8144 | } |
Chet Haase | f2f7d8f | 2010-12-03 14:08:14 -0800 | [diff] [blame] | 8145 | } |
Michael Jurka | ebefea4 | 2010-11-15 16:04:01 -0800 | [diff] [blame] | 8146 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8147 | if (p != null && ai != null) { |
| 8148 | final Rect r = ai.mTmpInvalRect; |
| 8149 | r.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 8150 | // Don't call invalidate -- we don't want to internally scroll |
| 8151 | // our own bounds |
| 8152 | p.invalidateChild(this, r); |
| 8153 | } |
| 8154 | } |
| 8155 | } |
| 8156 | |
| 8157 | /** |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 8158 | * @hide |
| 8159 | */ |
| 8160 | public void fastInvalidate() { |
| 8161 | if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) || |
| 8162 | (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID || |
| 8163 | (mPrivateFlags & INVALIDATED) != INVALIDATED) { |
| 8164 | if (mParent instanceof View) { |
| 8165 | ((View) mParent).mPrivateFlags |= INVALIDATED; |
| 8166 | } |
| 8167 | mPrivateFlags &= ~DRAWN; |
| 8168 | mPrivateFlags |= INVALIDATED; |
| 8169 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
Michael Jurka | d0872bd | 2011-03-24 15:44:19 -0700 | [diff] [blame] | 8170 | if (mParent != null && mAttachInfo != null) { |
| 8171 | if (mAttachInfo.mHardwareAccelerated) { |
| 8172 | mParent.invalidateChild(this, null); |
| 8173 | } else { |
| 8174 | final Rect r = mAttachInfo.mTmpInvalRect; |
| 8175 | r.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 8176 | // Don't call invalidate -- we don't want to internally scroll |
| 8177 | // our own bounds |
| 8178 | mParent.invalidateChild(this, r); |
| 8179 | } |
Romain Guy | da48979 | 2011-02-03 01:05:15 -0800 | [diff] [blame] | 8180 | } |
| 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | /** |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8185 | * Used to indicate that the parent of this view should clear its caches. This functionality |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8186 | * is used to force the parent to rebuild its display list (when hardware-accelerated), |
| 8187 | * which is necessary when various parent-managed properties of the view change, such as |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8188 | * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only |
| 8189 | * clears the parent caches and does not causes an invalidate event. |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8190 | * |
| 8191 | * @hide |
| 8192 | */ |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8193 | protected void invalidateParentCaches() { |
| 8194 | if (mParent instanceof View) { |
| 8195 | ((View) mParent).mPrivateFlags |= INVALIDATED; |
| 8196 | } |
| 8197 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8198 | |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8199 | /** |
| 8200 | * Used to indicate that the parent of this view should be invalidated. This functionality |
| 8201 | * is used to force the parent to rebuild its display list (when hardware-accelerated), |
| 8202 | * which is necessary when various parent-managed properties of the view change, such as |
| 8203 | * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate |
| 8204 | * an invalidation event to the parent. |
| 8205 | * |
| 8206 | * @hide |
| 8207 | */ |
| 8208 | protected void invalidateParentIfNeeded() { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8209 | if (isHardwareAccelerated() && mParent instanceof View) { |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 8210 | ((View) mParent).invalidate(true); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 8211 | } |
| 8212 | } |
| 8213 | |
| 8214 | /** |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 8215 | * Indicates whether this View is opaque. An opaque View guarantees that it will |
| 8216 | * draw all the pixels overlapping its bounds using a fully opaque color. |
| 8217 | * |
| 8218 | * Subclasses of View should override this method whenever possible to indicate |
| 8219 | * whether an instance is opaque. Opaque Views are treated in a special way by |
| 8220 | * the View hierarchy, possibly allowing it to perform optimizations during |
| 8221 | * invalidate/draw passes. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 8222 | * |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 8223 | * @return True if this View is guaranteed to be fully opaque, false otherwise. |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 8224 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 8225 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 8226 | public boolean isOpaque() { |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 8227 | return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK && |
| 8228 | (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD); |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 8229 | } |
| 8230 | |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 8231 | /** |
| 8232 | * @hide |
| 8233 | */ |
| 8234 | protected void computeOpaqueFlags() { |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 8235 | // Opaque if: |
| 8236 | // - Has a background |
| 8237 | // - Background is opaque |
| 8238 | // - Doesn't have scrollbars or scrollbars are inside overlay |
| 8239 | |
| 8240 | if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) { |
| 8241 | mPrivateFlags |= OPAQUE_BACKGROUND; |
| 8242 | } else { |
| 8243 | mPrivateFlags &= ~OPAQUE_BACKGROUND; |
| 8244 | } |
| 8245 | |
| 8246 | final int flags = mViewFlags; |
| 8247 | if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) || |
| 8248 | (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) { |
| 8249 | mPrivateFlags |= OPAQUE_SCROLLBARS; |
| 8250 | } else { |
| 8251 | mPrivateFlags &= ~OPAQUE_SCROLLBARS; |
| 8252 | } |
| 8253 | } |
| 8254 | |
| 8255 | /** |
| 8256 | * @hide |
| 8257 | */ |
| 8258 | protected boolean hasOpaqueScrollbars() { |
| 8259 | return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 8260 | } |
| 8261 | |
| 8262 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8263 | * @return A handler associated with the thread running the View. This |
| 8264 | * handler can be used to pump events in the UI events queue. |
| 8265 | */ |
| 8266 | public Handler getHandler() { |
| 8267 | if (mAttachInfo != null) { |
| 8268 | return mAttachInfo.mHandler; |
| 8269 | } |
| 8270 | return null; |
| 8271 | } |
| 8272 | |
| 8273 | /** |
| 8274 | * Causes the Runnable to be added to the message queue. |
| 8275 | * The runnable will be run on the user interface thread. |
| 8276 | * |
| 8277 | * @param action The Runnable that will be executed. |
| 8278 | * |
| 8279 | * @return Returns true if the Runnable was successfully placed in to the |
| 8280 | * message queue. Returns false on failure, usually because the |
| 8281 | * looper processing the message queue is exiting. |
| 8282 | */ |
| 8283 | public boolean post(Runnable action) { |
| 8284 | Handler handler; |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8285 | AttachInfo attachInfo = mAttachInfo; |
| 8286 | if (attachInfo != null) { |
| 8287 | handler = attachInfo.mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8288 | } else { |
| 8289 | // Assume that post will succeed later |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 8290 | ViewRootImpl.getRunQueue().post(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8291 | return true; |
| 8292 | } |
| 8293 | |
| 8294 | return handler.post(action); |
| 8295 | } |
| 8296 | |
| 8297 | /** |
| 8298 | * Causes the Runnable to be added to the message queue, to be run |
| 8299 | * after the specified amount of time elapses. |
| 8300 | * The runnable will be run on the user interface thread. |
| 8301 | * |
| 8302 | * @param action The Runnable that will be executed. |
| 8303 | * @param delayMillis The delay (in milliseconds) until the Runnable |
| 8304 | * will be executed. |
| 8305 | * |
| 8306 | * @return true if the Runnable was successfully placed in to the |
| 8307 | * message queue. Returns false on failure, usually because the |
| 8308 | * looper processing the message queue is exiting. Note that a |
| 8309 | * result of true does not mean the Runnable will be processed -- |
| 8310 | * if the looper is quit before the delivery time of the message |
| 8311 | * occurs then the message will be dropped. |
| 8312 | */ |
| 8313 | public boolean postDelayed(Runnable action, long delayMillis) { |
| 8314 | Handler handler; |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8315 | AttachInfo attachInfo = mAttachInfo; |
| 8316 | if (attachInfo != null) { |
| 8317 | handler = attachInfo.mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8318 | } else { |
| 8319 | // Assume that post will succeed later |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 8320 | ViewRootImpl.getRunQueue().postDelayed(action, delayMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8321 | return true; |
| 8322 | } |
| 8323 | |
| 8324 | return handler.postDelayed(action, delayMillis); |
| 8325 | } |
| 8326 | |
| 8327 | /** |
| 8328 | * Removes the specified Runnable from the message queue. |
| 8329 | * |
| 8330 | * @param action The Runnable to remove from the message handling queue |
| 8331 | * |
| 8332 | * @return true if this view could ask the Handler to remove the Runnable, |
| 8333 | * false otherwise. When the returned value is true, the Runnable |
| 8334 | * may or may not have been actually removed from the message queue |
| 8335 | * (for instance, if the Runnable was not in the queue already.) |
| 8336 | */ |
| 8337 | public boolean removeCallbacks(Runnable action) { |
| 8338 | Handler handler; |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8339 | AttachInfo attachInfo = mAttachInfo; |
| 8340 | if (attachInfo != null) { |
| 8341 | handler = attachInfo.mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8342 | } else { |
| 8343 | // Assume that post will succeed later |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 8344 | ViewRootImpl.getRunQueue().removeCallbacks(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8345 | return true; |
| 8346 | } |
| 8347 | |
| 8348 | handler.removeCallbacks(action); |
| 8349 | return true; |
| 8350 | } |
| 8351 | |
| 8352 | /** |
| 8353 | * Cause an invalidate to happen on a subsequent cycle through the event loop. |
| 8354 | * Use this to invalidate the View from a non-UI thread. |
| 8355 | * |
| 8356 | * @see #invalidate() |
| 8357 | */ |
| 8358 | public void postInvalidate() { |
| 8359 | postInvalidateDelayed(0); |
| 8360 | } |
| 8361 | |
| 8362 | /** |
| 8363 | * Cause an invalidate of the specified area to happen on a subsequent cycle |
| 8364 | * through the event loop. Use this to invalidate the View from a non-UI thread. |
| 8365 | * |
| 8366 | * @param left The left coordinate of the rectangle to invalidate. |
| 8367 | * @param top The top coordinate of the rectangle to invalidate. |
| 8368 | * @param right The right coordinate of the rectangle to invalidate. |
| 8369 | * @param bottom The bottom coordinate of the rectangle to invalidate. |
| 8370 | * |
| 8371 | * @see #invalidate(int, int, int, int) |
| 8372 | * @see #invalidate(Rect) |
| 8373 | */ |
| 8374 | public void postInvalidate(int left, int top, int right, int bottom) { |
| 8375 | postInvalidateDelayed(0, left, top, right, bottom); |
| 8376 | } |
| 8377 | |
| 8378 | /** |
| 8379 | * Cause an invalidate to happen on a subsequent cycle through the event |
| 8380 | * loop. Waits for the specified amount of time. |
| 8381 | * |
| 8382 | * @param delayMilliseconds the duration in milliseconds to delay the |
| 8383 | * invalidation by |
| 8384 | */ |
| 8385 | public void postInvalidateDelayed(long delayMilliseconds) { |
| 8386 | // We try only with the AttachInfo because there's no point in invalidating |
| 8387 | // if we are not attached to our window |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8388 | AttachInfo attachInfo = mAttachInfo; |
| 8389 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8390 | Message msg = Message.obtain(); |
| 8391 | msg.what = AttachInfo.INVALIDATE_MSG; |
| 8392 | msg.obj = this; |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8393 | attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8394 | } |
| 8395 | } |
| 8396 | |
| 8397 | /** |
| 8398 | * Cause an invalidate of the specified area to happen on a subsequent cycle |
| 8399 | * through the event loop. Waits for the specified amount of time. |
| 8400 | * |
| 8401 | * @param delayMilliseconds the duration in milliseconds to delay the |
| 8402 | * invalidation by |
| 8403 | * @param left The left coordinate of the rectangle to invalidate. |
| 8404 | * @param top The top coordinate of the rectangle to invalidate. |
| 8405 | * @param right The right coordinate of the rectangle to invalidate. |
| 8406 | * @param bottom The bottom coordinate of the rectangle to invalidate. |
| 8407 | */ |
| 8408 | public void postInvalidateDelayed(long delayMilliseconds, int left, int top, |
| 8409 | int right, int bottom) { |
| 8410 | |
| 8411 | // We try only with the AttachInfo because there's no point in invalidating |
| 8412 | // if we are not attached to our window |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8413 | AttachInfo attachInfo = mAttachInfo; |
| 8414 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8415 | final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire(); |
| 8416 | info.target = this; |
| 8417 | info.left = left; |
| 8418 | info.top = top; |
| 8419 | info.right = right; |
| 8420 | info.bottom = bottom; |
| 8421 | |
| 8422 | final Message msg = Message.obtain(); |
| 8423 | msg.what = AttachInfo.INVALIDATE_RECT_MSG; |
| 8424 | msg.obj = info; |
Romain Guy | c5a43a2 | 2011-03-24 13:28:56 -0700 | [diff] [blame] | 8425 | attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8426 | } |
| 8427 | } |
| 8428 | |
| 8429 | /** |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 8430 | * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event. |
| 8431 | * This event is sent at most once every |
| 8432 | * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}. |
| 8433 | */ |
| 8434 | private void postSendViewScrolledAccessibilityEventCallback() { |
| 8435 | if (mSendViewScrolledAccessibilityEvent == null) { |
| 8436 | mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent(); |
| 8437 | } |
| 8438 | if (!mSendViewScrolledAccessibilityEvent.mIsPending) { |
| 8439 | mSendViewScrolledAccessibilityEvent.mIsPending = true; |
| 8440 | postDelayed(mSendViewScrolledAccessibilityEvent, |
| 8441 | ViewConfiguration.getSendRecurringAccessibilityEventsInterval()); |
| 8442 | } |
| 8443 | } |
| 8444 | |
| 8445 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8446 | * Called by a parent to request that a child update its values for mScrollX |
| 8447 | * and mScrollY if necessary. This will typically be done if the child is |
| 8448 | * animating a scroll using a {@link android.widget.Scroller Scroller} |
| 8449 | * object. |
| 8450 | */ |
| 8451 | public void computeScroll() { |
| 8452 | } |
| 8453 | |
| 8454 | /** |
| 8455 | * <p>Indicate whether the horizontal edges are faded when the view is |
| 8456 | * scrolled horizontally.</p> |
| 8457 | * |
| 8458 | * @return true if the horizontal edges should are faded on scroll, false |
| 8459 | * otherwise |
| 8460 | * |
| 8461 | * @see #setHorizontalFadingEdgeEnabled(boolean) |
| 8462 | * @attr ref android.R.styleable#View_fadingEdge |
| 8463 | */ |
| 8464 | public boolean isHorizontalFadingEdgeEnabled() { |
| 8465 | return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL; |
| 8466 | } |
| 8467 | |
| 8468 | /** |
| 8469 | * <p>Define whether the horizontal edges should be faded when this view |
| 8470 | * is scrolled horizontally.</p> |
| 8471 | * |
| 8472 | * @param horizontalFadingEdgeEnabled true if the horizontal edges should |
| 8473 | * be faded when the view is scrolled |
| 8474 | * horizontally |
| 8475 | * |
| 8476 | * @see #isHorizontalFadingEdgeEnabled() |
| 8477 | * @attr ref android.R.styleable#View_fadingEdge |
| 8478 | */ |
| 8479 | public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) { |
| 8480 | if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) { |
| 8481 | if (horizontalFadingEdgeEnabled) { |
| 8482 | initScrollCache(); |
| 8483 | } |
| 8484 | |
| 8485 | mViewFlags ^= FADING_EDGE_HORIZONTAL; |
| 8486 | } |
| 8487 | } |
| 8488 | |
| 8489 | /** |
| 8490 | * <p>Indicate whether the vertical edges are faded when the view is |
| 8491 | * scrolled horizontally.</p> |
| 8492 | * |
| 8493 | * @return true if the vertical edges should are faded on scroll, false |
| 8494 | * otherwise |
| 8495 | * |
| 8496 | * @see #setVerticalFadingEdgeEnabled(boolean) |
| 8497 | * @attr ref android.R.styleable#View_fadingEdge |
| 8498 | */ |
| 8499 | public boolean isVerticalFadingEdgeEnabled() { |
| 8500 | return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL; |
| 8501 | } |
| 8502 | |
| 8503 | /** |
| 8504 | * <p>Define whether the vertical edges should be faded when this view |
| 8505 | * is scrolled vertically.</p> |
| 8506 | * |
| 8507 | * @param verticalFadingEdgeEnabled true if the vertical edges should |
| 8508 | * be faded when the view is scrolled |
| 8509 | * vertically |
| 8510 | * |
| 8511 | * @see #isVerticalFadingEdgeEnabled() |
| 8512 | * @attr ref android.R.styleable#View_fadingEdge |
| 8513 | */ |
| 8514 | public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) { |
| 8515 | if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) { |
| 8516 | if (verticalFadingEdgeEnabled) { |
| 8517 | initScrollCache(); |
| 8518 | } |
| 8519 | |
| 8520 | mViewFlags ^= FADING_EDGE_VERTICAL; |
| 8521 | } |
| 8522 | } |
| 8523 | |
| 8524 | /** |
| 8525 | * Returns the strength, or intensity, of the top faded edge. The strength is |
| 8526 | * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation |
| 8527 | * returns 0.0 or 1.0 but no value in between. |
| 8528 | * |
| 8529 | * Subclasses should override this method to provide a smoother fade transition |
| 8530 | * when scrolling occurs. |
| 8531 | * |
| 8532 | * @return the intensity of the top fade as a float between 0.0f and 1.0f |
| 8533 | */ |
| 8534 | protected float getTopFadingEdgeStrength() { |
| 8535 | return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f; |
| 8536 | } |
| 8537 | |
| 8538 | /** |
| 8539 | * Returns the strength, or intensity, of the bottom faded edge. The strength is |
| 8540 | * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation |
| 8541 | * returns 0.0 or 1.0 but no value in between. |
| 8542 | * |
| 8543 | * Subclasses should override this method to provide a smoother fade transition |
| 8544 | * when scrolling occurs. |
| 8545 | * |
| 8546 | * @return the intensity of the bottom fade as a float between 0.0f and 1.0f |
| 8547 | */ |
| 8548 | protected float getBottomFadingEdgeStrength() { |
| 8549 | return computeVerticalScrollOffset() + computeVerticalScrollExtent() < |
| 8550 | computeVerticalScrollRange() ? 1.0f : 0.0f; |
| 8551 | } |
| 8552 | |
| 8553 | /** |
| 8554 | * Returns the strength, or intensity, of the left faded edge. The strength is |
| 8555 | * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation |
| 8556 | * returns 0.0 or 1.0 but no value in between. |
| 8557 | * |
| 8558 | * Subclasses should override this method to provide a smoother fade transition |
| 8559 | * when scrolling occurs. |
| 8560 | * |
| 8561 | * @return the intensity of the left fade as a float between 0.0f and 1.0f |
| 8562 | */ |
| 8563 | protected float getLeftFadingEdgeStrength() { |
| 8564 | return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f; |
| 8565 | } |
| 8566 | |
| 8567 | /** |
| 8568 | * Returns the strength, or intensity, of the right faded edge. The strength is |
| 8569 | * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation |
| 8570 | * returns 0.0 or 1.0 but no value in between. |
| 8571 | * |
| 8572 | * Subclasses should override this method to provide a smoother fade transition |
| 8573 | * when scrolling occurs. |
| 8574 | * |
| 8575 | * @return the intensity of the right fade as a float between 0.0f and 1.0f |
| 8576 | */ |
| 8577 | protected float getRightFadingEdgeStrength() { |
| 8578 | return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() < |
| 8579 | computeHorizontalScrollRange() ? 1.0f : 0.0f; |
| 8580 | } |
| 8581 | |
| 8582 | /** |
| 8583 | * <p>Indicate whether the horizontal scrollbar should be drawn or not. The |
| 8584 | * scrollbar is not drawn by default.</p> |
| 8585 | * |
| 8586 | * @return true if the horizontal scrollbar should be painted, false |
| 8587 | * otherwise |
| 8588 | * |
| 8589 | * @see #setHorizontalScrollBarEnabled(boolean) |
| 8590 | */ |
| 8591 | public boolean isHorizontalScrollBarEnabled() { |
| 8592 | return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL; |
| 8593 | } |
| 8594 | |
| 8595 | /** |
| 8596 | * <p>Define whether the horizontal scrollbar should be drawn or not. The |
| 8597 | * scrollbar is not drawn by default.</p> |
| 8598 | * |
| 8599 | * @param horizontalScrollBarEnabled true if the horizontal scrollbar should |
| 8600 | * be painted |
| 8601 | * |
| 8602 | * @see #isHorizontalScrollBarEnabled() |
| 8603 | */ |
| 8604 | public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) { |
| 8605 | if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) { |
| 8606 | mViewFlags ^= SCROLLBARS_HORIZONTAL; |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 8607 | computeOpaqueFlags(); |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 8608 | resolvePadding(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8609 | } |
| 8610 | } |
| 8611 | |
| 8612 | /** |
| 8613 | * <p>Indicate whether the vertical scrollbar should be drawn or not. The |
| 8614 | * scrollbar is not drawn by default.</p> |
| 8615 | * |
| 8616 | * @return true if the vertical scrollbar should be painted, false |
| 8617 | * otherwise |
| 8618 | * |
| 8619 | * @see #setVerticalScrollBarEnabled(boolean) |
| 8620 | */ |
| 8621 | public boolean isVerticalScrollBarEnabled() { |
| 8622 | return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL; |
| 8623 | } |
| 8624 | |
| 8625 | /** |
| 8626 | * <p>Define whether the vertical scrollbar should be drawn or not. The |
| 8627 | * scrollbar is not drawn by default.</p> |
| 8628 | * |
| 8629 | * @param verticalScrollBarEnabled true if the vertical scrollbar should |
| 8630 | * be painted |
| 8631 | * |
| 8632 | * @see #isVerticalScrollBarEnabled() |
| 8633 | */ |
| 8634 | public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) { |
| 8635 | if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) { |
| 8636 | mViewFlags ^= SCROLLBARS_VERTICAL; |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 8637 | computeOpaqueFlags(); |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 8638 | resolvePadding(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8639 | } |
| 8640 | } |
| 8641 | |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 8642 | /** |
| 8643 | * @hide |
| 8644 | */ |
| 8645 | protected void recomputePadding() { |
| 8646 | setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8647 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8648 | |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 8649 | /** |
| 8650 | * Define whether scrollbars will fade when the view is not scrolling. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8651 | * |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 8652 | * @param fadeScrollbars wheter to enable fading |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8653 | * |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 8654 | */ |
| 8655 | public void setScrollbarFadingEnabled(boolean fadeScrollbars) { |
| 8656 | initScrollCache(); |
| 8657 | final ScrollabilityCache scrollabilityCache = mScrollCache; |
| 8658 | scrollabilityCache.fadeScrollBars = fadeScrollbars; |
Mike Cleron | 52f0a64 | 2009-09-28 18:21:37 -0700 | [diff] [blame] | 8659 | if (fadeScrollbars) { |
| 8660 | scrollabilityCache.state = ScrollabilityCache.OFF; |
| 8661 | } else { |
Mike Cleron | fe81d38 | 2009-09-28 14:22:16 -0700 | [diff] [blame] | 8662 | scrollabilityCache.state = ScrollabilityCache.ON; |
| 8663 | } |
| 8664 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8666 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8667 | * |
Mike Cleron | 52f0a64 | 2009-09-28 18:21:37 -0700 | [diff] [blame] | 8668 | * Returns true if scrollbars will fade when this view is not scrolling |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8669 | * |
Mike Cleron | 52f0a64 | 2009-09-28 18:21:37 -0700 | [diff] [blame] | 8670 | * @return true if scrollbar fading is enabled |
| 8671 | */ |
| 8672 | public boolean isScrollbarFadingEnabled() { |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8673 | return mScrollCache != null && mScrollCache.fadeScrollBars; |
Mike Cleron | 52f0a64 | 2009-09-28 18:21:37 -0700 | [diff] [blame] | 8674 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8675 | |
Mike Cleron | 52f0a64 | 2009-09-28 18:21:37 -0700 | [diff] [blame] | 8676 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8677 | * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or |
| 8678 | * inset. When inset, they add to the padding of the view. And the scrollbars |
| 8679 | * can be drawn inside the padding area or on the edge of the view. For example, |
| 8680 | * if a view has a background drawable and you want to draw the scrollbars |
| 8681 | * inside the padding specified by the drawable, you can use |
| 8682 | * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to |
| 8683 | * appear at the edge of the view, ignoring the padding, then you can use |
| 8684 | * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p> |
| 8685 | * @param style the style of the scrollbars. Should be one of |
| 8686 | * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET, |
| 8687 | * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET. |
| 8688 | * @see #SCROLLBARS_INSIDE_OVERLAY |
| 8689 | * @see #SCROLLBARS_INSIDE_INSET |
| 8690 | * @see #SCROLLBARS_OUTSIDE_OVERLAY |
| 8691 | * @see #SCROLLBARS_OUTSIDE_INSET |
| 8692 | */ |
| 8693 | public void setScrollBarStyle(int style) { |
| 8694 | if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) { |
| 8695 | mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK); |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 8696 | computeOpaqueFlags(); |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 8697 | resolvePadding(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | /** |
| 8702 | * <p>Returns the current scrollbar style.</p> |
| 8703 | * @return the current scrollbar style |
| 8704 | * @see #SCROLLBARS_INSIDE_OVERLAY |
| 8705 | * @see #SCROLLBARS_INSIDE_INSET |
| 8706 | * @see #SCROLLBARS_OUTSIDE_OVERLAY |
| 8707 | * @see #SCROLLBARS_OUTSIDE_INSET |
| 8708 | */ |
| 8709 | public int getScrollBarStyle() { |
| 8710 | return mViewFlags & SCROLLBARS_STYLE_MASK; |
| 8711 | } |
| 8712 | |
| 8713 | /** |
| 8714 | * <p>Compute the horizontal range that the horizontal scrollbar |
| 8715 | * represents.</p> |
| 8716 | * |
| 8717 | * <p>The range is expressed in arbitrary units that must be the same as the |
| 8718 | * units used by {@link #computeHorizontalScrollExtent()} and |
| 8719 | * {@link #computeHorizontalScrollOffset()}.</p> |
| 8720 | * |
| 8721 | * <p>The default range is the drawing width of this view.</p> |
| 8722 | * |
| 8723 | * @return the total horizontal range represented by the horizontal |
| 8724 | * scrollbar |
| 8725 | * |
| 8726 | * @see #computeHorizontalScrollExtent() |
| 8727 | * @see #computeHorizontalScrollOffset() |
| 8728 | * @see android.widget.ScrollBarDrawable |
| 8729 | */ |
| 8730 | protected int computeHorizontalScrollRange() { |
| 8731 | return getWidth(); |
| 8732 | } |
| 8733 | |
| 8734 | /** |
| 8735 | * <p>Compute the horizontal offset of the horizontal scrollbar's thumb |
| 8736 | * within the horizontal range. This value is used to compute the position |
| 8737 | * of the thumb within the scrollbar's track.</p> |
| 8738 | * |
| 8739 | * <p>The range is expressed in arbitrary units that must be the same as the |
| 8740 | * units used by {@link #computeHorizontalScrollRange()} and |
| 8741 | * {@link #computeHorizontalScrollExtent()}.</p> |
| 8742 | * |
| 8743 | * <p>The default offset is the scroll offset of this view.</p> |
| 8744 | * |
| 8745 | * @return the horizontal offset of the scrollbar's thumb |
| 8746 | * |
| 8747 | * @see #computeHorizontalScrollRange() |
| 8748 | * @see #computeHorizontalScrollExtent() |
| 8749 | * @see android.widget.ScrollBarDrawable |
| 8750 | */ |
| 8751 | protected int computeHorizontalScrollOffset() { |
| 8752 | return mScrollX; |
| 8753 | } |
| 8754 | |
| 8755 | /** |
| 8756 | * <p>Compute the horizontal extent of the horizontal scrollbar's thumb |
| 8757 | * within the horizontal range. This value is used to compute the length |
| 8758 | * of the thumb within the scrollbar's track.</p> |
| 8759 | * |
| 8760 | * <p>The range is expressed in arbitrary units that must be the same as the |
| 8761 | * units used by {@link #computeHorizontalScrollRange()} and |
| 8762 | * {@link #computeHorizontalScrollOffset()}.</p> |
| 8763 | * |
| 8764 | * <p>The default extent is the drawing width of this view.</p> |
| 8765 | * |
| 8766 | * @return the horizontal extent of the scrollbar's thumb |
| 8767 | * |
| 8768 | * @see #computeHorizontalScrollRange() |
| 8769 | * @see #computeHorizontalScrollOffset() |
| 8770 | * @see android.widget.ScrollBarDrawable |
| 8771 | */ |
| 8772 | protected int computeHorizontalScrollExtent() { |
| 8773 | return getWidth(); |
| 8774 | } |
| 8775 | |
| 8776 | /** |
| 8777 | * <p>Compute the vertical range that the vertical scrollbar represents.</p> |
| 8778 | * |
| 8779 | * <p>The range is expressed in arbitrary units that must be the same as the |
| 8780 | * units used by {@link #computeVerticalScrollExtent()} and |
| 8781 | * {@link #computeVerticalScrollOffset()}.</p> |
| 8782 | * |
| 8783 | * @return the total vertical range represented by the vertical scrollbar |
| 8784 | * |
| 8785 | * <p>The default range is the drawing height of this view.</p> |
| 8786 | * |
| 8787 | * @see #computeVerticalScrollExtent() |
| 8788 | * @see #computeVerticalScrollOffset() |
| 8789 | * @see android.widget.ScrollBarDrawable |
| 8790 | */ |
| 8791 | protected int computeVerticalScrollRange() { |
| 8792 | return getHeight(); |
| 8793 | } |
| 8794 | |
| 8795 | /** |
| 8796 | * <p>Compute the vertical offset of the vertical scrollbar's thumb |
| 8797 | * within the horizontal range. This value is used to compute the position |
| 8798 | * of the thumb within the scrollbar's track.</p> |
| 8799 | * |
| 8800 | * <p>The range is expressed in arbitrary units that must be the same as the |
| 8801 | * units used by {@link #computeVerticalScrollRange()} and |
| 8802 | * {@link #computeVerticalScrollExtent()}.</p> |
| 8803 | * |
| 8804 | * <p>The default offset is the scroll offset of this view.</p> |
| 8805 | * |
| 8806 | * @return the vertical offset of the scrollbar's thumb |
| 8807 | * |
| 8808 | * @see #computeVerticalScrollRange() |
| 8809 | * @see #computeVerticalScrollExtent() |
| 8810 | * @see android.widget.ScrollBarDrawable |
| 8811 | */ |
| 8812 | protected int computeVerticalScrollOffset() { |
| 8813 | return mScrollY; |
| 8814 | } |
| 8815 | |
| 8816 | /** |
| 8817 | * <p>Compute the vertical extent of the horizontal scrollbar's thumb |
| 8818 | * within the vertical range. This value is used to compute the length |
| 8819 | * of the thumb within the scrollbar's track.</p> |
| 8820 | * |
| 8821 | * <p>The range is expressed in arbitrary units that must be the same as the |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 8822 | * units used by {@link #computeVerticalScrollRange()} and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8823 | * {@link #computeVerticalScrollOffset()}.</p> |
| 8824 | * |
| 8825 | * <p>The default extent is the drawing height of this view.</p> |
| 8826 | * |
| 8827 | * @return the vertical extent of the scrollbar's thumb |
| 8828 | * |
| 8829 | * @see #computeVerticalScrollRange() |
| 8830 | * @see #computeVerticalScrollOffset() |
| 8831 | * @see android.widget.ScrollBarDrawable |
| 8832 | */ |
| 8833 | protected int computeVerticalScrollExtent() { |
| 8834 | return getHeight(); |
| 8835 | } |
| 8836 | |
| 8837 | /** |
Adam Powell | 6915944 | 2011-06-13 17:53:06 -0700 | [diff] [blame] | 8838 | * Check if this view can be scrolled horizontally in a certain direction. |
| 8839 | * |
| 8840 | * @param direction Negative to check scrolling left, positive to check scrolling right. |
| 8841 | * @return true if this view can be scrolled in the specified direction, false otherwise. |
| 8842 | */ |
| 8843 | public boolean canScrollHorizontally(int direction) { |
| 8844 | final int offset = computeHorizontalScrollOffset(); |
| 8845 | final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent(); |
| 8846 | if (range == 0) return false; |
| 8847 | if (direction < 0) { |
| 8848 | return offset > 0; |
| 8849 | } else { |
| 8850 | return offset < range - 1; |
| 8851 | } |
| 8852 | } |
| 8853 | |
| 8854 | /** |
| 8855 | * Check if this view can be scrolled vertically in a certain direction. |
| 8856 | * |
| 8857 | * @param direction Negative to check scrolling up, positive to check scrolling down. |
| 8858 | * @return true if this view can be scrolled in the specified direction, false otherwise. |
| 8859 | */ |
| 8860 | public boolean canScrollVertically(int direction) { |
| 8861 | final int offset = computeVerticalScrollOffset(); |
| 8862 | final int range = computeVerticalScrollRange() - computeVerticalScrollExtent(); |
| 8863 | if (range == 0) return false; |
| 8864 | if (direction < 0) { |
| 8865 | return offset > 0; |
| 8866 | } else { |
| 8867 | return offset < range - 1; |
| 8868 | } |
| 8869 | } |
| 8870 | |
| 8871 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8872 | * <p>Request the drawing of the horizontal and the vertical scrollbar. The |
| 8873 | * scrollbars are painted only if they have been awakened first.</p> |
| 8874 | * |
| 8875 | * @param canvas the canvas on which to draw the scrollbars |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8876 | * |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8877 | * @see #awakenScrollBars(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8878 | */ |
Romain Guy | 1d5b3a6 | 2009-11-05 18:44:12 -0800 | [diff] [blame] | 8879 | protected final void onDrawScrollBars(Canvas canvas) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8880 | // scrollbars are drawn only when the animation is running |
| 8881 | final ScrollabilityCache cache = mScrollCache; |
| 8882 | if (cache != null) { |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8883 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8884 | int state = cache.state; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8885 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8886 | if (state == ScrollabilityCache.OFF) { |
| 8887 | return; |
| 8888 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8889 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8890 | boolean invalidate = false; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8891 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8892 | if (state == ScrollabilityCache.FADING) { |
| 8893 | // We're fading -- get our fade interpolation |
| 8894 | if (cache.interpolatorValues == null) { |
| 8895 | cache.interpolatorValues = new float[1]; |
| 8896 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8897 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8898 | float[] values = cache.interpolatorValues; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8899 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8900 | // Stops the animation if we're done |
| 8901 | if (cache.scrollBarInterpolator.timeToValues(values) == |
| 8902 | Interpolator.Result.FREEZE_END) { |
| 8903 | cache.state = ScrollabilityCache.OFF; |
| 8904 | } else { |
| 8905 | cache.scrollBar.setAlpha(Math.round(values[0])); |
| 8906 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8907 | |
| 8908 | // This will make the scroll bars inval themselves after |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8909 | // drawing. We only want this when we're fading so that |
| 8910 | // we prevent excessive redraws |
| 8911 | invalidate = true; |
| 8912 | } else { |
| 8913 | // We're just on -- but we may have been fading before so |
| 8914 | // reset alpha |
| 8915 | cache.scrollBar.setAlpha(255); |
| 8916 | } |
| 8917 | |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8918 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8919 | final int viewFlags = mViewFlags; |
| 8920 | |
| 8921 | final boolean drawHorizontalScrollBar = |
| 8922 | (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL; |
| 8923 | final boolean drawVerticalScrollBar = |
| 8924 | (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL |
| 8925 | && !isVerticalScrollBarHidden(); |
| 8926 | |
| 8927 | if (drawVerticalScrollBar || drawHorizontalScrollBar) { |
| 8928 | final int width = mRight - mLeft; |
| 8929 | final int height = mBottom - mTop; |
| 8930 | |
| 8931 | final ScrollBarDrawable scrollBar = cache.scrollBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8932 | |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 8933 | final int scrollX = mScrollX; |
| 8934 | final int scrollY = mScrollY; |
| 8935 | final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0; |
| 8936 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8937 | int left, top, right, bottom; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8939 | if (drawHorizontalScrollBar) { |
Adam Powell | 3ba6774 | 2011-01-27 14:16:55 -0800 | [diff] [blame] | 8940 | int size = scrollBar.getSize(false); |
| 8941 | if (size <= 0) { |
| 8942 | size = cache.scrollBarSize; |
| 8943 | } |
| 8944 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8945 | scrollBar.setParameters(computeHorizontalScrollRange(), |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 8946 | computeHorizontalScrollOffset(), |
| 8947 | computeHorizontalScrollExtent(), false); |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 8948 | final int verticalScrollBarGap = drawVerticalScrollBar ? |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8949 | getVerticalScrollbarWidth() : 0; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 8950 | top = scrollY + height - size - (mUserPaddingBottom & inside); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8951 | left = scrollX + (mPaddingLeft & inside); |
| 8952 | right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap; |
| 8953 | bottom = top + size; |
| 8954 | onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom); |
| 8955 | if (invalidate) { |
| 8956 | invalidate(left, top, right, bottom); |
| 8957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8958 | } |
| 8959 | |
| 8960 | if (drawVerticalScrollBar) { |
Adam Powell | 3ba6774 | 2011-01-27 14:16:55 -0800 | [diff] [blame] | 8961 | int size = scrollBar.getSize(true); |
| 8962 | if (size <= 0) { |
| 8963 | size = cache.scrollBarSize; |
| 8964 | } |
| 8965 | |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 8966 | scrollBar.setParameters(computeVerticalScrollRange(), |
| 8967 | computeVerticalScrollOffset(), |
| 8968 | computeVerticalScrollExtent(), true); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 8969 | switch (mVerticalScrollbarPosition) { |
| 8970 | default: |
| 8971 | case SCROLLBAR_POSITION_DEFAULT: |
| 8972 | case SCROLLBAR_POSITION_RIGHT: |
| 8973 | left = scrollX + width - size - (mUserPaddingRight & inside); |
| 8974 | break; |
| 8975 | case SCROLLBAR_POSITION_LEFT: |
| 8976 | left = scrollX + (mUserPaddingLeft & inside); |
| 8977 | break; |
| 8978 | } |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 8979 | top = scrollY + (mPaddingTop & inside); |
| 8980 | right = left + size; |
| 8981 | bottom = scrollY + height - (mUserPaddingBottom & inside); |
| 8982 | onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom); |
| 8983 | if (invalidate) { |
| 8984 | invalidate(left, top, right, bottom); |
| 8985 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8986 | } |
| 8987 | } |
| 8988 | } |
| 8989 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 8990 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8991 | /** |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 8992 | * Override this if the vertical scrollbar needs to be hidden in a subclass, like when |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8993 | * FastScroller is visible. |
| 8994 | * @return whether to temporarily hide the vertical scrollbar |
| 8995 | * @hide |
| 8996 | */ |
| 8997 | protected boolean isVerticalScrollBarHidden() { |
| 8998 | return false; |
| 8999 | } |
| 9000 | |
| 9001 | /** |
| 9002 | * <p>Draw the horizontal scrollbar if |
| 9003 | * {@link #isHorizontalScrollBarEnabled()} returns true.</p> |
| 9004 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9005 | * @param canvas the canvas on which to draw the scrollbar |
| 9006 | * @param scrollBar the scrollbar's drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9007 | * |
| 9008 | * @see #isHorizontalScrollBarEnabled() |
| 9009 | * @see #computeHorizontalScrollRange() |
| 9010 | * @see #computeHorizontalScrollExtent() |
| 9011 | * @see #computeHorizontalScrollOffset() |
| 9012 | * @see android.widget.ScrollBarDrawable |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 9013 | * @hide |
Mike Reed | 4d6fe5f | 2009-09-03 13:29:05 -0400 | [diff] [blame] | 9014 | */ |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 9015 | protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar, |
| 9016 | int l, int t, int r, int b) { |
Mike Reed | 4d6fe5f | 2009-09-03 13:29:05 -0400 | [diff] [blame] | 9017 | scrollBar.setBounds(l, t, r, b); |
Mike Reed | 4d6fe5f | 2009-09-03 13:29:05 -0400 | [diff] [blame] | 9018 | scrollBar.draw(canvas); |
| 9019 | } |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 9020 | |
Mike Reed | 4d6fe5f | 2009-09-03 13:29:05 -0400 | [diff] [blame] | 9021 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9022 | * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()} |
| 9023 | * returns true.</p> |
| 9024 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9025 | * @param canvas the canvas on which to draw the scrollbar |
| 9026 | * @param scrollBar the scrollbar's drawable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9027 | * |
| 9028 | * @see #isVerticalScrollBarEnabled() |
| 9029 | * @see #computeVerticalScrollRange() |
| 9030 | * @see #computeVerticalScrollExtent() |
| 9031 | * @see #computeVerticalScrollOffset() |
| 9032 | * @see android.widget.ScrollBarDrawable |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 9033 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9034 | */ |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 9035 | protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, |
| 9036 | int l, int t, int r, int b) { |
Mike Reed | e8853fc | 2009-09-04 14:01:48 -0400 | [diff] [blame] | 9037 | scrollBar.setBounds(l, t, r, b); |
| 9038 | scrollBar.draw(canvas); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9039 | } |
| 9040 | |
| 9041 | /** |
| 9042 | * Implement this to do your drawing. |
| 9043 | * |
| 9044 | * @param canvas the canvas on which the background will be drawn |
| 9045 | */ |
| 9046 | protected void onDraw(Canvas canvas) { |
| 9047 | } |
| 9048 | |
| 9049 | /* |
| 9050 | * Caller is responsible for calling requestLayout if necessary. |
| 9051 | * (This allows addViewInLayout to not request a new layout.) |
| 9052 | */ |
| 9053 | void assignParent(ViewParent parent) { |
| 9054 | if (mParent == null) { |
| 9055 | mParent = parent; |
| 9056 | } else if (parent == null) { |
| 9057 | mParent = null; |
| 9058 | } else { |
| 9059 | throw new RuntimeException("view " + this + " being added, but" |
| 9060 | + " it already has a parent"); |
| 9061 | } |
| 9062 | } |
| 9063 | |
| 9064 | /** |
| 9065 | * This is called when the view is attached to a window. At this point it |
| 9066 | * has a Surface and will start drawing. Note that this function is |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9067 | * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)}, |
| 9068 | * however it may be called any time before the first onDraw -- including |
| 9069 | * before or after {@link #onMeasure(int, int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9070 | * |
| 9071 | * @see #onDetachedFromWindow() |
| 9072 | */ |
| 9073 | protected void onAttachedToWindow() { |
| 9074 | if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) { |
| 9075 | mParent.requestTransparentRegion(this); |
| 9076 | } |
Adam Powell | 8568c3a | 2010-04-19 14:26:11 -0700 | [diff] [blame] | 9077 | if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) { |
| 9078 | initialAwakenScrollBars(); |
| 9079 | mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH; |
| 9080 | } |
Chet Haase | a9b61ac | 2010-12-20 07:40:25 -0800 | [diff] [blame] | 9081 | jumpDrawablesToCurrentState(); |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9082 | resolveLayoutDirectionIfNeeded(); |
| 9083 | resolvePadding(); |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 9084 | resolveTextDirection(); |
Amith Yamasani | 4503c8d | 2011-06-17 12:36:14 -0700 | [diff] [blame] | 9085 | if (isFocused()) { |
| 9086 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 9087 | imm.focusIn(this); |
| 9088 | } |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 9089 | } |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 9090 | |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 9091 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9092 | * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing |
| 9093 | * that the parent directionality can and will be resolved before its children. |
Fabrice Di Meglio | 4f5aa91 | 2011-05-31 15:20:50 -0700 | [diff] [blame] | 9094 | */ |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9095 | private void resolveLayoutDirectionIfNeeded() { |
| 9096 | // Do not resolve if it is not needed |
| 9097 | if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return; |
| 9098 | |
| 9099 | // Clear any previous layout direction resolution |
| 9100 | mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL; |
| 9101 | |
| 9102 | // Set resolved depending on layout direction |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 9103 | switch (getLayoutDirection()) { |
| 9104 | case LAYOUT_DIRECTION_INHERIT: |
Fabrice Di Meglio | fe7e40d | 2011-07-13 12:47:36 -0700 | [diff] [blame] | 9105 | // We cannot do the resolution if there is no parent |
| 9106 | if (mParent == null) return; |
| 9107 | |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 9108 | // If this is root view, no need to look at parent's layout dir. |
Fabrice Di Meglio | fe7e40d | 2011-07-13 12:47:36 -0700 | [diff] [blame] | 9109 | if (mParent instanceof ViewGroup) { |
| 9110 | ViewGroup viewGroup = ((ViewGroup) mParent); |
| 9111 | |
| 9112 | // Check if the parent view group can resolve |
| 9113 | if (! viewGroup.canResolveLayoutDirection()) { |
| 9114 | return; |
| 9115 | } |
| 9116 | if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) { |
| 9117 | mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL; |
| 9118 | } |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 9119 | } |
| 9120 | break; |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 9121 | case LAYOUT_DIRECTION_RTL: |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9122 | mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL; |
Cibu Johny | 8666663 | 2010-02-22 13:01:02 -0800 | [diff] [blame] | 9123 | break; |
Fabrice Di Meglio | 26e432d | 2011-06-10 14:19:18 -0700 | [diff] [blame] | 9124 | case LAYOUT_DIRECTION_LOCALE: |
| 9125 | if(isLayoutDirectionRtl(Locale.getDefault())) { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9126 | mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL; |
Fabrice Di Meglio | 26e432d | 2011-06-10 14:19:18 -0700 | [diff] [blame] | 9127 | } |
| 9128 | break; |
| 9129 | default: |
| 9130 | // Nothing to do, LTR by default |
| 9131 | } |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9132 | |
| 9133 | // Set to resolved |
| 9134 | mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED; |
| 9135 | } |
| 9136 | |
Fabrice Di Meglio | aff599b | 2011-07-20 19:05:01 -0700 | [diff] [blame] | 9137 | /** |
| 9138 | * @hide |
| 9139 | */ |
| 9140 | protected void resolvePadding() { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9141 | // If the user specified the absolute padding (either with android:padding or |
| 9142 | // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise |
| 9143 | // use the default padding or the padding from the background drawable |
| 9144 | // (stored at this point in mPadding*) |
| 9145 | switch (getResolvedLayoutDirection()) { |
| 9146 | case LAYOUT_DIRECTION_RTL: |
| 9147 | // Start user padding override Right user padding. Otherwise, if Right user |
| 9148 | // padding is not defined, use the default Right padding. If Right user padding |
| 9149 | // is defined, just use it. |
| 9150 | if (mUserPaddingStart >= 0) { |
| 9151 | mUserPaddingRight = mUserPaddingStart; |
| 9152 | } else if (mUserPaddingRight < 0) { |
| 9153 | mUserPaddingRight = mPaddingRight; |
| 9154 | } |
| 9155 | if (mUserPaddingEnd >= 0) { |
| 9156 | mUserPaddingLeft = mUserPaddingEnd; |
| 9157 | } else if (mUserPaddingLeft < 0) { |
| 9158 | mUserPaddingLeft = mPaddingLeft; |
| 9159 | } |
| 9160 | break; |
| 9161 | case LAYOUT_DIRECTION_LTR: |
| 9162 | default: |
| 9163 | // Start user padding override Left user padding. Otherwise, if Left user |
| 9164 | // padding is not defined, use the default left padding. If Left user padding |
| 9165 | // is defined, just use it. |
Fabrice Di Meglio | f3e1a93 | 2011-07-15 17:15:39 -0700 | [diff] [blame] | 9166 | if (mUserPaddingStart >= 0) { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9167 | mUserPaddingLeft = mUserPaddingStart; |
| 9168 | } else if (mUserPaddingLeft < 0) { |
| 9169 | mUserPaddingLeft = mPaddingLeft; |
| 9170 | } |
Fabrice Di Meglio | f3e1a93 | 2011-07-15 17:15:39 -0700 | [diff] [blame] | 9171 | if (mUserPaddingEnd >= 0) { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9172 | mUserPaddingRight = mUserPaddingEnd; |
| 9173 | } else if (mUserPaddingRight < 0) { |
| 9174 | mUserPaddingRight = mPaddingRight; |
| 9175 | } |
| 9176 | } |
| 9177 | |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9178 | mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom; |
| 9179 | |
| 9180 | recomputePadding(); |
| 9181 | } |
| 9182 | |
Fabrice Di Meglio | fe7e40d | 2011-07-13 12:47:36 -0700 | [diff] [blame] | 9183 | protected boolean canResolveLayoutDirection() { |
| 9184 | switch (getLayoutDirection()) { |
| 9185 | case LAYOUT_DIRECTION_INHERIT: |
| 9186 | return (mParent != null); |
| 9187 | default: |
| 9188 | return true; |
| 9189 | } |
| 9190 | } |
| 9191 | |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9192 | /** |
Doug Felt | c0ccf0c | 2011-06-23 16:13:18 -0700 | [diff] [blame] | 9193 | * Reset the resolved layout direction. |
| 9194 | * |
| 9195 | * Subclasses need to override this method to clear cached information that depends on the |
| 9196 | * resolved layout direction, or to inform child views that inherit their layout direction. |
| 9197 | * Overrides must also call the superclass implementation at the start of their implementation. |
| 9198 | * |
| 9199 | * @hide |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9200 | */ |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 9201 | protected void resetResolvedLayoutDirection() { |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 9202 | // Reset the current View resolution |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 9203 | mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED; |
Fabrice Di Meglio | 26e432d | 2011-06-10 14:19:18 -0700 | [diff] [blame] | 9204 | } |
| 9205 | |
| 9206 | /** |
| 9207 | * Check if a Locale is corresponding to a RTL script. |
| 9208 | * |
| 9209 | * @param locale Locale to check |
| 9210 | * @return true if a Locale is corresponding to a RTL script. |
| 9211 | */ |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 9212 | protected static boolean isLayoutDirectionRtl(Locale locale) { |
Fabrice Di Meglio | a47f45e | 2011-06-15 14:22:12 -0700 | [diff] [blame] | 9213 | return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE == |
| 9214 | LocaleUtil.getLayoutDirectionFromLocale(locale)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | /** |
| 9218 | * This is called when the view is detached from a window. At this point it |
| 9219 | * no longer has a surface for drawing. |
| 9220 | * |
| 9221 | * @see #onAttachedToWindow() |
| 9222 | */ |
| 9223 | protected void onDetachedFromWindow() { |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 9224 | mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9225 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 9226 | removeUnsetPressCallback(); |
Maryam Garrett | 1549dd1 | 2009-12-15 16:06:36 -0500 | [diff] [blame] | 9227 | removeLongPressCallback(); |
Adam Powell | 3cb8b63 | 2011-01-21 15:34:14 -0800 | [diff] [blame] | 9228 | removePerformClickCallback(); |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 9229 | removeSendViewScrolledAccessibilityEventCallback(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9231 | destroyDrawingCache(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9232 | |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9233 | destroyLayer(); |
Romain Guy | 8dd5b1e | 2011-01-14 17:28:51 -0800 | [diff] [blame] | 9234 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9235 | if (mDisplayList != null) { |
| 9236 | mDisplayList.invalidate(); |
| 9237 | } |
| 9238 | |
Romain Guy | 8dd5b1e | 2011-01-14 17:28:51 -0800 | [diff] [blame] | 9239 | if (mAttachInfo != null) { |
| 9240 | mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this); |
| 9241 | mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this); |
| 9242 | } |
| 9243 | |
Patrick Dubroy | ec84c3a | 2011-01-13 17:55:37 -0800 | [diff] [blame] | 9244 | mCurrentAnimation = null; |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 9245 | |
| 9246 | resetResolvedLayoutDirection(); |
| 9247 | resetResolvedTextDirection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9248 | } |
| 9249 | |
| 9250 | /** |
| 9251 | * @return The number of times this view has been attached to a window |
| 9252 | */ |
| 9253 | protected int getWindowAttachCount() { |
| 9254 | return mWindowAttachCount; |
| 9255 | } |
| 9256 | |
| 9257 | /** |
| 9258 | * Retrieve a unique token identifying the window this view is attached to. |
| 9259 | * @return Return the window's token for use in |
| 9260 | * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}. |
| 9261 | */ |
| 9262 | public IBinder getWindowToken() { |
| 9263 | return mAttachInfo != null ? mAttachInfo.mWindowToken : null; |
| 9264 | } |
| 9265 | |
| 9266 | /** |
| 9267 | * Retrieve a unique token identifying the top-level "real" window of |
| 9268 | * the window that this view is attached to. That is, this is like |
| 9269 | * {@link #getWindowToken}, except if the window this view in is a panel |
| 9270 | * window (attached to another containing window), then the token of |
| 9271 | * the containing window is returned instead. |
| 9272 | * |
| 9273 | * @return Returns the associated window token, either |
| 9274 | * {@link #getWindowToken()} or the containing window's token. |
| 9275 | */ |
| 9276 | public IBinder getApplicationWindowToken() { |
| 9277 | AttachInfo ai = mAttachInfo; |
| 9278 | if (ai != null) { |
| 9279 | IBinder appWindowToken = ai.mPanelParentWindowToken; |
| 9280 | if (appWindowToken == null) { |
| 9281 | appWindowToken = ai.mWindowToken; |
| 9282 | } |
| 9283 | return appWindowToken; |
| 9284 | } |
| 9285 | return null; |
| 9286 | } |
| 9287 | |
| 9288 | /** |
| 9289 | * Retrieve private session object this view hierarchy is using to |
| 9290 | * communicate with the window manager. |
| 9291 | * @return the session object to communicate with the window manager |
| 9292 | */ |
| 9293 | /*package*/ IWindowSession getWindowSession() { |
| 9294 | return mAttachInfo != null ? mAttachInfo.mSession : null; |
| 9295 | } |
| 9296 | |
| 9297 | /** |
| 9298 | * @param info the {@link android.view.View.AttachInfo} to associated with |
| 9299 | * this view |
| 9300 | */ |
| 9301 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
| 9302 | //System.out.println("Attached! " + this); |
| 9303 | mAttachInfo = info; |
| 9304 | mWindowAttachCount++; |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 9305 | // We will need to evaluate the drawable state at least once. |
| 9306 | mPrivateFlags |= DRAWABLE_STATE_DIRTY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9307 | if (mFloatingTreeObserver != null) { |
| 9308 | info.mTreeObserver.merge(mFloatingTreeObserver); |
| 9309 | mFloatingTreeObserver = null; |
| 9310 | } |
| 9311 | if ((mPrivateFlags&SCROLL_CONTAINER) != 0) { |
| 9312 | mAttachInfo.mScrollContainers.add(this); |
| 9313 | mPrivateFlags |= SCROLL_CONTAINER_ADDED; |
| 9314 | } |
| 9315 | performCollectViewAttributes(visibility); |
| 9316 | onAttachedToWindow(); |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 9317 | |
| 9318 | final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners = |
| 9319 | mOnAttachStateChangeListeners; |
| 9320 | if (listeners != null && listeners.size() > 0) { |
| 9321 | // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to |
| 9322 | // perform the dispatching. The iterator is a safe guard against listeners that |
| 9323 | // could mutate the list by calling the various add/remove methods. This prevents |
| 9324 | // the array from being modified while we iterate it. |
| 9325 | for (OnAttachStateChangeListener listener : listeners) { |
| 9326 | listener.onViewAttachedToWindow(this); |
| 9327 | } |
| 9328 | } |
| 9329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9330 | int vis = info.mWindowVisibility; |
| 9331 | if (vis != GONE) { |
| 9332 | onWindowVisibilityChanged(vis); |
| 9333 | } |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 9334 | if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) { |
| 9335 | // If nobody has evaluated the drawable state yet, then do it now. |
| 9336 | refreshDrawableState(); |
| 9337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9338 | } |
| 9339 | |
| 9340 | void dispatchDetachedFromWindow() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9341 | AttachInfo info = mAttachInfo; |
| 9342 | if (info != null) { |
| 9343 | int vis = info.mWindowVisibility; |
| 9344 | if (vis != GONE) { |
| 9345 | onWindowVisibilityChanged(GONE); |
| 9346 | } |
| 9347 | } |
| 9348 | |
| 9349 | onDetachedFromWindow(); |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 9350 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 9351 | final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners = |
| 9352 | mOnAttachStateChangeListeners; |
| 9353 | if (listeners != null && listeners.size() > 0) { |
| 9354 | // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to |
| 9355 | // perform the dispatching. The iterator is a safe guard against listeners that |
| 9356 | // could mutate the list by calling the various add/remove methods. This prevents |
| 9357 | // the array from being modified while we iterate it. |
| 9358 | for (OnAttachStateChangeListener listener : listeners) { |
| 9359 | listener.onViewDetachedFromWindow(this); |
| 9360 | } |
| 9361 | } |
| 9362 | |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 9363 | if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9364 | mAttachInfo.mScrollContainers.remove(this); |
| 9365 | mPrivateFlags &= ~SCROLL_CONTAINER_ADDED; |
| 9366 | } |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 9367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9368 | mAttachInfo = null; |
| 9369 | } |
| 9370 | |
| 9371 | /** |
| 9372 | * Store this view hierarchy's frozen state into the given container. |
| 9373 | * |
| 9374 | * @param container The SparseArray in which to save the view's state. |
| 9375 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9376 | * @see #restoreHierarchyState(android.util.SparseArray) |
| 9377 | * @see #dispatchSaveInstanceState(android.util.SparseArray) |
| 9378 | * @see #onSaveInstanceState() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9379 | */ |
| 9380 | public void saveHierarchyState(SparseArray<Parcelable> container) { |
| 9381 | dispatchSaveInstanceState(container); |
| 9382 | } |
| 9383 | |
| 9384 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9385 | * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for |
| 9386 | * this view and its children. May be overridden to modify how freezing happens to a |
| 9387 | * view's children; for example, some views may want to not store state for their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9388 | * |
| 9389 | * @param container The SparseArray in which to save the view's state. |
| 9390 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9391 | * @see #dispatchRestoreInstanceState(android.util.SparseArray) |
| 9392 | * @see #saveHierarchyState(android.util.SparseArray) |
| 9393 | * @see #onSaveInstanceState() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9394 | */ |
| 9395 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 9396 | if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) { |
| 9397 | mPrivateFlags &= ~SAVE_STATE_CALLED; |
| 9398 | Parcelable state = onSaveInstanceState(); |
| 9399 | if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) { |
| 9400 | throw new IllegalStateException( |
| 9401 | "Derived class did not call super.onSaveInstanceState()"); |
| 9402 | } |
| 9403 | if (state != null) { |
| 9404 | // Log.i("View", "Freezing #" + Integer.toHexString(mID) |
| 9405 | // + ": " + state); |
| 9406 | container.put(mID, state); |
| 9407 | } |
| 9408 | } |
| 9409 | } |
| 9410 | |
| 9411 | /** |
| 9412 | * Hook allowing a view to generate a representation of its internal state |
| 9413 | * that can later be used to create a new instance with that same state. |
| 9414 | * This state should only contain information that is not persistent or can |
| 9415 | * not be reconstructed later. For example, you will never store your |
| 9416 | * current position on screen because that will be computed again when a |
| 9417 | * new instance of the view is placed in its view hierarchy. |
| 9418 | * <p> |
| 9419 | * Some examples of things you may store here: the current cursor position |
| 9420 | * in a text view (but usually not the text itself since that is stored in a |
| 9421 | * content provider or other persistent storage), the currently selected |
| 9422 | * item in a list view. |
| 9423 | * |
| 9424 | * @return Returns a Parcelable object containing the view's current dynamic |
| 9425 | * state, or null if there is nothing interesting to save. The |
| 9426 | * default implementation returns null. |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9427 | * @see #onRestoreInstanceState(android.os.Parcelable) |
| 9428 | * @see #saveHierarchyState(android.util.SparseArray) |
| 9429 | * @see #dispatchSaveInstanceState(android.util.SparseArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9430 | * @see #setSaveEnabled(boolean) |
| 9431 | */ |
| 9432 | protected Parcelable onSaveInstanceState() { |
| 9433 | mPrivateFlags |= SAVE_STATE_CALLED; |
| 9434 | return BaseSavedState.EMPTY_STATE; |
| 9435 | } |
| 9436 | |
| 9437 | /** |
| 9438 | * Restore this view hierarchy's frozen state from the given container. |
| 9439 | * |
| 9440 | * @param container The SparseArray which holds previously frozen states. |
| 9441 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9442 | * @see #saveHierarchyState(android.util.SparseArray) |
| 9443 | * @see #dispatchRestoreInstanceState(android.util.SparseArray) |
| 9444 | * @see #onRestoreInstanceState(android.os.Parcelable) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9445 | */ |
| 9446 | public void restoreHierarchyState(SparseArray<Parcelable> container) { |
| 9447 | dispatchRestoreInstanceState(container); |
| 9448 | } |
| 9449 | |
| 9450 | /** |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9451 | * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the |
| 9452 | * state for this view and its children. May be overridden to modify how restoring |
| 9453 | * happens to a view's children; for example, some views may want to not store state |
| 9454 | * for their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9455 | * |
| 9456 | * @param container The SparseArray which holds previously saved state. |
| 9457 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9458 | * @see #dispatchSaveInstanceState(android.util.SparseArray) |
| 9459 | * @see #restoreHierarchyState(android.util.SparseArray) |
| 9460 | * @see #onRestoreInstanceState(android.os.Parcelable) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9461 | */ |
| 9462 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 9463 | if (mID != NO_ID) { |
| 9464 | Parcelable state = container.get(mID); |
| 9465 | if (state != null) { |
| 9466 | // Log.i("View", "Restoreing #" + Integer.toHexString(mID) |
| 9467 | // + ": " + state); |
| 9468 | mPrivateFlags &= ~SAVE_STATE_CALLED; |
| 9469 | onRestoreInstanceState(state); |
| 9470 | if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) { |
| 9471 | throw new IllegalStateException( |
| 9472 | "Derived class did not call super.onRestoreInstanceState()"); |
| 9473 | } |
| 9474 | } |
| 9475 | } |
| 9476 | } |
| 9477 | |
| 9478 | /** |
| 9479 | * Hook allowing a view to re-apply a representation of its internal state that had previously |
| 9480 | * been generated by {@link #onSaveInstanceState}. This function will never be called with a |
| 9481 | * null state. |
| 9482 | * |
| 9483 | * @param state The frozen state that had previously been returned by |
| 9484 | * {@link #onSaveInstanceState}. |
| 9485 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 9486 | * @see #onSaveInstanceState() |
| 9487 | * @see #restoreHierarchyState(android.util.SparseArray) |
| 9488 | * @see #dispatchRestoreInstanceState(android.util.SparseArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9489 | */ |
| 9490 | protected void onRestoreInstanceState(Parcelable state) { |
| 9491 | mPrivateFlags |= SAVE_STATE_CALLED; |
| 9492 | if (state != BaseSavedState.EMPTY_STATE && state != null) { |
Romain Guy | 237c1ce | 2009-12-08 11:30:25 -0800 | [diff] [blame] | 9493 | throw new IllegalArgumentException("Wrong state class, expecting View State but " |
| 9494 | + "received " + state.getClass().toString() + " instead. This usually happens " |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9495 | + "when two views of different type have the same id in the same hierarchy. " |
| 9496 | + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure " |
Romain Guy | 237c1ce | 2009-12-08 11:30:25 -0800 | [diff] [blame] | 9497 | + "other views do not use the same id."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9498 | } |
| 9499 | } |
| 9500 | |
| 9501 | /** |
| 9502 | * <p>Return the time at which the drawing of the view hierarchy started.</p> |
| 9503 | * |
| 9504 | * @return the drawing start time in milliseconds |
| 9505 | */ |
| 9506 | public long getDrawingTime() { |
| 9507 | return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0; |
| 9508 | } |
| 9509 | |
| 9510 | /** |
| 9511 | * <p>Enables or disables the duplication of the parent's state into this view. When |
| 9512 | * duplication is enabled, this view gets its drawable state from its parent rather |
| 9513 | * than from its own internal properties.</p> |
| 9514 | * |
| 9515 | * <p>Note: in the current implementation, setting this property to true after the |
| 9516 | * view was added to a ViewGroup might have no effect at all. This property should |
| 9517 | * always be used from XML or set to true before adding this view to a ViewGroup.</p> |
| 9518 | * |
| 9519 | * <p>Note: if this view's parent addStateFromChildren property is enabled and this |
| 9520 | * property is enabled, an exception will be thrown.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9521 | * |
Gilles Debunne | fb81703 | 2011-01-13 13:52:49 -0800 | [diff] [blame] | 9522 | * <p>Note: if the child view uses and updates additionnal states which are unknown to the |
| 9523 | * parent, these states should not be affected by this method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9524 | * |
| 9525 | * @param enabled True to enable duplication of the parent's drawable state, false |
| 9526 | * to disable it. |
| 9527 | * |
| 9528 | * @see #getDrawableState() |
| 9529 | * @see #isDuplicateParentStateEnabled() |
| 9530 | */ |
| 9531 | public void setDuplicateParentStateEnabled(boolean enabled) { |
| 9532 | setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE); |
| 9533 | } |
| 9534 | |
| 9535 | /** |
| 9536 | * <p>Indicates whether this duplicates its drawable state from its parent.</p> |
| 9537 | * |
| 9538 | * @return True if this view's drawable state is duplicated from the parent, |
| 9539 | * false otherwise |
| 9540 | * |
| 9541 | * @see #getDrawableState() |
| 9542 | * @see #setDuplicateParentStateEnabled(boolean) |
| 9543 | */ |
| 9544 | public boolean isDuplicateParentStateEnabled() { |
| 9545 | return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE; |
| 9546 | } |
| 9547 | |
| 9548 | /** |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9549 | * <p>Specifies the type of layer backing this view. The layer can be |
| 9550 | * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or |
| 9551 | * {@link #LAYER_TYPE_HARDWARE hardware}.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9552 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9553 | * <p>A layer is associated with an optional {@link android.graphics.Paint} |
| 9554 | * instance that controls how the layer is composed on screen. The following |
| 9555 | * properties of the paint are taken into account when composing the layer:</p> |
| 9556 | * <ul> |
| 9557 | * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li> |
| 9558 | * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li> |
| 9559 | * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li> |
| 9560 | * </ul> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9561 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9562 | * <p>If this view has an alpha value set to < 1.0 by calling |
| 9563 | * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by |
| 9564 | * this view's alpha value. Calling {@link #setAlpha(float)} is therefore |
| 9565 | * equivalent to setting a hardware layer on this view and providing a paint with |
| 9566 | * the desired alpha value.<p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9567 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9568 | * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled}, |
| 9569 | * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware} |
| 9570 | * for more information on when and how to use layers.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9571 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9572 | * @param layerType The ype of layer to use with this view, must be one of |
| 9573 | * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or |
| 9574 | * {@link #LAYER_TYPE_HARDWARE} |
| 9575 | * @param paint The paint used to compose the layer. This argument is optional |
| 9576 | * and can be null. It is ignored when the layer type is |
| 9577 | * {@link #LAYER_TYPE_NONE} |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9578 | * |
| 9579 | * @see #getLayerType() |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9580 | * @see #LAYER_TYPE_NONE |
| 9581 | * @see #LAYER_TYPE_SOFTWARE |
| 9582 | * @see #LAYER_TYPE_HARDWARE |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9583 | * @see #setAlpha(float) |
| 9584 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9585 | * @attr ref android.R.styleable#View_layerType |
| 9586 | */ |
| 9587 | public void setLayerType(int layerType, Paint paint) { |
| 9588 | if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) { |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9589 | throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, " |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9590 | + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE"); |
| 9591 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9592 | |
Romain Guy | d6cd572 | 2011-01-17 14:42:41 -0800 | [diff] [blame] | 9593 | if (layerType == mLayerType) { |
| 9594 | if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) { |
| 9595 | mLayerPaint = paint == null ? new Paint() : paint; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 9596 | invalidateParentCaches(); |
| 9597 | invalidate(true); |
Romain Guy | d6cd572 | 2011-01-17 14:42:41 -0800 | [diff] [blame] | 9598 | } |
| 9599 | return; |
| 9600 | } |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9601 | |
| 9602 | // Destroy any previous software drawing cache if needed |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9603 | switch (mLayerType) { |
Chet Haase | 6f33e81 | 2011-05-17 12:42:19 -0700 | [diff] [blame] | 9604 | case LAYER_TYPE_HARDWARE: |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9605 | destroyLayer(); |
Chet Haase | 6f33e81 | 2011-05-17 12:42:19 -0700 | [diff] [blame] | 9606 | // fall through - unaccelerated views may use software layer mechanism instead |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9607 | case LAYER_TYPE_SOFTWARE: |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9608 | destroyDrawingCache(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9609 | break; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9610 | default: |
| 9611 | break; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9612 | } |
| 9613 | |
| 9614 | mLayerType = layerType; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 9615 | final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE; |
| 9616 | mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint); |
| 9617 | mLocalDirtyRect = layerDisabled ? null : new Rect(); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9618 | |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 9619 | invalidateParentCaches(); |
| 9620 | invalidate(true); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9621 | } |
| 9622 | |
| 9623 | /** |
| 9624 | * Indicates what type of layer is currently associated with this view. By default |
| 9625 | * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}. |
| 9626 | * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)} |
| 9627 | * for more information on the different types of layers. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9628 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9629 | * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or |
| 9630 | * {@link #LAYER_TYPE_HARDWARE} |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9631 | * |
| 9632 | * @see #setLayerType(int, android.graphics.Paint) |
Romain Guy | f1ae106 | 2011-03-02 18:16:04 -0800 | [diff] [blame] | 9633 | * @see #buildLayer() |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9634 | * @see #LAYER_TYPE_NONE |
| 9635 | * @see #LAYER_TYPE_SOFTWARE |
| 9636 | * @see #LAYER_TYPE_HARDWARE |
| 9637 | */ |
| 9638 | public int getLayerType() { |
| 9639 | return mLayerType; |
| 9640 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9641 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9642 | /** |
Romain Guy | f1ae106 | 2011-03-02 18:16:04 -0800 | [diff] [blame] | 9643 | * Forces this view's layer to be created and this view to be rendered |
| 9644 | * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE}, |
| 9645 | * invoking this method will have no effect. |
| 9646 | * |
| 9647 | * This method can for instance be used to render a view into its layer before |
| 9648 | * starting an animation. If this view is complex, rendering into the layer |
| 9649 | * before starting the animation will avoid skipping frames. |
| 9650 | * |
| 9651 | * @throws IllegalStateException If this view is not attached to a window |
| 9652 | * |
| 9653 | * @see #setLayerType(int, android.graphics.Paint) |
| 9654 | */ |
| 9655 | public void buildLayer() { |
| 9656 | if (mLayerType == LAYER_TYPE_NONE) return; |
| 9657 | |
| 9658 | if (mAttachInfo == null) { |
| 9659 | throw new IllegalStateException("This view must be attached to a window first"); |
| 9660 | } |
| 9661 | |
| 9662 | switch (mLayerType) { |
| 9663 | case LAYER_TYPE_HARDWARE: |
| 9664 | getHardwareLayer(); |
| 9665 | break; |
| 9666 | case LAYER_TYPE_SOFTWARE: |
| 9667 | buildDrawingCache(true); |
| 9668 | break; |
| 9669 | } |
| 9670 | } |
| 9671 | |
| 9672 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9673 | * <p>Returns a hardware layer that can be used to draw this view again |
| 9674 | * without executing its draw method.</p> |
| 9675 | * |
| 9676 | * @return A HardwareLayer ready to render, or null if an error occurred. |
| 9677 | */ |
Romain Guy | 5e7f766 | 2011-01-24 22:35:56 -0800 | [diff] [blame] | 9678 | HardwareLayer getHardwareLayer() { |
Romain Guy | ea83503 | 2011-07-28 19:24:37 -0700 | [diff] [blame^] | 9679 | if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null || |
| 9680 | !mAttachInfo.mHardwareRenderer.isEnabled()) { |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9681 | return null; |
| 9682 | } |
| 9683 | |
| 9684 | final int width = mRight - mLeft; |
| 9685 | final int height = mBottom - mTop; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9686 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9687 | if (width == 0 || height == 0) { |
| 9688 | return null; |
| 9689 | } |
| 9690 | |
| 9691 | if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) { |
| 9692 | if (mHardwareLayer == null) { |
| 9693 | mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer( |
| 9694 | width, height, isOpaque()); |
Romain Guy | 62687ec | 2011-02-02 15:44:19 -0800 | [diff] [blame] | 9695 | mLocalDirtyRect.setEmpty(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9696 | } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) { |
| 9697 | mHardwareLayer.resize(width, height); |
Romain Guy | 62687ec | 2011-02-02 15:44:19 -0800 | [diff] [blame] | 9698 | mLocalDirtyRect.setEmpty(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9699 | } |
| 9700 | |
Romain Guy | 59a12ca | 2011-06-09 17:48:21 -0700 | [diff] [blame] | 9701 | HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas; |
Romain Guy | 5e7f766 | 2011-01-24 22:35:56 -0800 | [diff] [blame] | 9702 | final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas); |
| 9703 | mAttachInfo.mHardwareCanvas = canvas; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9704 | try { |
| 9705 | canvas.setViewport(width, height); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 9706 | canvas.onPreDraw(mLocalDirtyRect); |
Romain Guy | 62687ec | 2011-02-02 15:44:19 -0800 | [diff] [blame] | 9707 | mLocalDirtyRect.setEmpty(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9708 | |
Chet Haase | 88172fe | 2011-03-07 17:36:33 -0800 | [diff] [blame] | 9709 | final int restoreCount = canvas.save(); |
| 9710 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9711 | computeScroll(); |
| 9712 | canvas.translate(-mScrollX, -mScrollY); |
| 9713 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9714 | mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9715 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9716 | // Fast path for layouts with no backgrounds |
| 9717 | if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) { |
| 9718 | mPrivateFlags &= ~DIRTY_MASK; |
| 9719 | dispatchDraw(canvas); |
| 9720 | } else { |
| 9721 | draw(canvas); |
| 9722 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9723 | |
Chet Haase | 88172fe | 2011-03-07 17:36:33 -0800 | [diff] [blame] | 9724 | canvas.restoreToCount(restoreCount); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9725 | } finally { |
| 9726 | canvas.onPostDraw(); |
Romain Guy | 5e7f766 | 2011-01-24 22:35:56 -0800 | [diff] [blame] | 9727 | mHardwareLayer.end(currentCanvas); |
| 9728 | mAttachInfo.mHardwareCanvas = currentCanvas; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 9729 | } |
| 9730 | } |
| 9731 | |
| 9732 | return mHardwareLayer; |
| 9733 | } |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9734 | |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 9735 | boolean destroyLayer() { |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9736 | if (mHardwareLayer != null) { |
| 9737 | mHardwareLayer.destroy(); |
| 9738 | mHardwareLayer = null; |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 9739 | return true; |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9740 | } |
Romain Guy | 65b345f | 2011-07-27 18:51:50 -0700 | [diff] [blame] | 9741 | return false; |
Romain Guy | 6d7475d | 2011-07-27 16:28:21 -0700 | [diff] [blame] | 9742 | } |
| 9743 | |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9744 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9745 | * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call |
| 9746 | * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a |
| 9747 | * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when |
| 9748 | * the cache is enabled. To benefit from the cache, you must request the drawing cache by |
| 9749 | * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not |
| 9750 | * null.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9751 | * |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 9752 | * <p>Enabling the drawing cache is similar to |
| 9753 | * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9754 | * acceleration is turned off. When hardware acceleration is turned on, enabling the |
| 9755 | * drawing cache has no effect on rendering because the system uses a different mechanism |
| 9756 | * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even |
| 9757 | * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)} |
| 9758 | * for information on how to enable software and hardware layers.</p> |
| 9759 | * |
| 9760 | * <p>This API can be used to manually generate |
| 9761 | * a bitmap copy of this view, by setting the flag to <code>true</code> and calling |
| 9762 | * {@link #getDrawingCache()}.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9763 | * |
| 9764 | * @param enabled true to enable the drawing cache, false otherwise |
| 9765 | * |
| 9766 | * @see #isDrawingCacheEnabled() |
| 9767 | * @see #getDrawingCache() |
| 9768 | * @see #buildDrawingCache() |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9769 | * @see #setLayerType(int, android.graphics.Paint) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9770 | */ |
| 9771 | public void setDrawingCacheEnabled(boolean enabled) { |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 9772 | mCachingFailed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9773 | setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED); |
| 9774 | } |
| 9775 | |
| 9776 | /** |
| 9777 | * <p>Indicates whether the drawing cache is enabled for this view.</p> |
| 9778 | * |
| 9779 | * @return true if the drawing cache is enabled |
| 9780 | * |
| 9781 | * @see #setDrawingCacheEnabled(boolean) |
| 9782 | * @see #getDrawingCache() |
| 9783 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 9784 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9785 | public boolean isDrawingCacheEnabled() { |
| 9786 | return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED; |
| 9787 | } |
| 9788 | |
| 9789 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9790 | * Debugging utility which recursively outputs the dirty state of a view and its |
| 9791 | * descendants. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9792 | * |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9793 | * @hide |
| 9794 | */ |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 9795 | @SuppressWarnings({"UnusedDeclaration"}) |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9796 | public void outputDirtyFlags(String indent, boolean clear, int clearMask) { |
| 9797 | Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) + |
| 9798 | ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" + |
| 9799 | (mPrivateFlags & View.DRAWING_CACHE_VALID) + |
| 9800 | ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")"); |
| 9801 | if (clear) { |
| 9802 | mPrivateFlags &= clearMask; |
| 9803 | } |
| 9804 | if (this instanceof ViewGroup) { |
| 9805 | ViewGroup parent = (ViewGroup) this; |
| 9806 | final int count = parent.getChildCount(); |
| 9807 | for (int i = 0; i < count; i++) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 9808 | final View child = parent.getChildAt(i); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9809 | child.outputDirtyFlags(indent + " ", clear, clearMask); |
| 9810 | } |
| 9811 | } |
| 9812 | } |
| 9813 | |
| 9814 | /** |
| 9815 | * This method is used by ViewGroup to cause its children to restore or recreate their |
| 9816 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 9817 | * to recreate its own display list, which would happen if it went through the normal |
| 9818 | * draw/dispatchDraw mechanisms. |
| 9819 | * |
| 9820 | * @hide |
| 9821 | */ |
| 9822 | protected void dispatchGetDisplayList() {} |
Chet Haase | f4ac547 | 2011-01-27 10:30:25 -0800 | [diff] [blame] | 9823 | |
| 9824 | /** |
| 9825 | * A view that is not attached or hardware accelerated cannot create a display list. |
| 9826 | * This method checks these conditions and returns the appropriate result. |
| 9827 | * |
| 9828 | * @return true if view has the ability to create a display list, false otherwise. |
| 9829 | * |
| 9830 | * @hide |
| 9831 | */ |
| 9832 | public boolean canHaveDisplayList() { |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 9833 | return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null); |
Chet Haase | f4ac547 | 2011-01-27 10:30:25 -0800 | [diff] [blame] | 9834 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9835 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9836 | /** |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9837 | * <p>Returns a display list that can be used to draw this view again |
| 9838 | * without executing its draw method.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9839 | * |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9840 | * @return A DisplayList ready to replay, or null if caching is not enabled. |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9841 | * |
| 9842 | * @hide |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9843 | */ |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9844 | public DisplayList getDisplayList() { |
Chet Haase | f4ac547 | 2011-01-27 10:30:25 -0800 | [diff] [blame] | 9845 | if (!canHaveDisplayList()) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9846 | return null; |
| 9847 | } |
| 9848 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9849 | if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || |
| 9850 | mDisplayList == null || !mDisplayList.isValid() || |
| 9851 | mRecreateDisplayList)) { |
| 9852 | // Don't need to recreate the display list, just need to tell our |
| 9853 | // children to restore/recreate theirs |
| 9854 | if (mDisplayList != null && mDisplayList.isValid() && |
| 9855 | !mRecreateDisplayList) { |
| 9856 | mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID; |
| 9857 | mPrivateFlags &= ~DIRTY_MASK; |
| 9858 | dispatchGetDisplayList(); |
| 9859 | |
| 9860 | return mDisplayList; |
| 9861 | } |
| 9862 | |
| 9863 | // If we got here, we're recreating it. Mark it as such to ensure that |
| 9864 | // we copy in child display lists into ours in drawChild() |
| 9865 | mRecreateDisplayList = true; |
Chet Haase | 9e90a99 | 2011-01-04 16:23:21 -0800 | [diff] [blame] | 9866 | if (mDisplayList == null) { |
Jeff Brown | 162a021 | 2011-07-21 17:02:54 -0700 | [diff] [blame] | 9867 | mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9868 | // If we're creating a new display list, make sure our parent gets invalidated |
| 9869 | // since they will need to recreate their display list to account for this |
| 9870 | // new child display list. |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 9871 | invalidateParentCaches(); |
Chet Haase | 9e90a99 | 2011-01-04 16:23:21 -0800 | [diff] [blame] | 9872 | } |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9873 | |
| 9874 | final HardwareCanvas canvas = mDisplayList.start(); |
| 9875 | try { |
| 9876 | int width = mRight - mLeft; |
| 9877 | int height = mBottom - mTop; |
| 9878 | |
| 9879 | canvas.setViewport(width, height); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 9880 | // The dirty rect should always be null for a display list |
| 9881 | canvas.onPreDraw(null); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9882 | |
Chet Haase | 88172fe | 2011-03-07 17:36:33 -0800 | [diff] [blame] | 9883 | final int restoreCount = canvas.save(); |
| 9884 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 9885 | computeScroll(); |
| 9886 | canvas.translate(-mScrollX, -mScrollY); |
Romain Guy | 2fe9a8f | 2010-10-04 20:17:01 -0700 | [diff] [blame] | 9887 | mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID; |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 9888 | mPrivateFlags &= ~DIRTY_MASK; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9889 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9890 | // Fast path for layouts with no backgrounds |
| 9891 | if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9892 | dispatchDraw(canvas); |
| 9893 | } else { |
| 9894 | draw(canvas); |
| 9895 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9896 | |
Chet Haase | 88172fe | 2011-03-07 17:36:33 -0800 | [diff] [blame] | 9897 | canvas.restoreToCount(restoreCount); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9898 | } finally { |
| 9899 | canvas.onPostDraw(); |
| 9900 | |
| 9901 | mDisplayList.end(); |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9902 | } |
Chet Haase | 77785f9 | 2011-01-25 23:22:09 -0800 | [diff] [blame] | 9903 | } else { |
| 9904 | mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID; |
| 9905 | mPrivateFlags &= ~DIRTY_MASK; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 9906 | } |
| 9907 | |
| 9908 | return mDisplayList; |
| 9909 | } |
| 9910 | |
| 9911 | /** |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9912 | * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9913 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9914 | * @return A non-scaled bitmap representing this view or null if cache is disabled. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9915 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9916 | * @see #getDrawingCache(boolean) |
| 9917 | */ |
| 9918 | public Bitmap getDrawingCache() { |
| 9919 | return getDrawingCache(false); |
| 9920 | } |
| 9921 | |
| 9922 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9923 | * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap |
| 9924 | * is null when caching is disabled. If caching is enabled and the cache is not ready, |
| 9925 | * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not |
| 9926 | * draw from the cache when the cache is enabled. To benefit from the cache, you must |
| 9927 | * request the drawing cache by calling this method and draw it on screen if the |
| 9928 | * returned bitmap is not null.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9929 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9930 | * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled, |
| 9931 | * this method will create a bitmap of the same size as this view. Because this bitmap |
| 9932 | * will be drawn scaled by the parent ViewGroup, the result on screen might show |
| 9933 | * scaling artifacts. To avoid such artifacts, you should call this method by setting |
| 9934 | * the auto scaling to true. Doing so, however, will generate a bitmap of a different |
| 9935 | * size than the view. This implies that your application must be able to handle this |
| 9936 | * size.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9937 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9938 | * @param autoScale Indicates whether the generated bitmap should be scaled based on |
| 9939 | * the current density of the screen when the application is in compatibility |
| 9940 | * mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9941 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9942 | * @return A bitmap representing this view or null if cache is disabled. |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 9943 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9944 | * @see #setDrawingCacheEnabled(boolean) |
| 9945 | * @see #isDrawingCacheEnabled() |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9946 | * @see #buildDrawingCache(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9947 | * @see #destroyDrawingCache() |
| 9948 | */ |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9949 | public Bitmap getDrawingCache(boolean autoScale) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9950 | if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) { |
| 9951 | return null; |
| 9952 | } |
| 9953 | if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) { |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9954 | buildDrawingCache(autoScale); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9955 | } |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 9956 | return autoScale ? mDrawingCache : mUnscaledDrawingCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9957 | } |
| 9958 | |
| 9959 | /** |
| 9960 | * <p>Frees the resources used by the drawing cache. If you call |
| 9961 | * {@link #buildDrawingCache()} manually without calling |
| 9962 | * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you |
| 9963 | * should cleanup the cache with this method afterwards.</p> |
| 9964 | * |
| 9965 | * @see #setDrawingCacheEnabled(boolean) |
| 9966 | * @see #buildDrawingCache() |
| 9967 | * @see #getDrawingCache() |
| 9968 | */ |
| 9969 | public void destroyDrawingCache() { |
| 9970 | if (mDrawingCache != null) { |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 9971 | mDrawingCache.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9972 | mDrawingCache = null; |
| 9973 | } |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9974 | if (mUnscaledDrawingCache != null) { |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 9975 | mUnscaledDrawingCache.recycle(); |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 9976 | mUnscaledDrawingCache = null; |
| 9977 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9978 | } |
| 9979 | |
| 9980 | /** |
| 9981 | * Setting a solid background color for the drawing cache's bitmaps will improve |
| 9982 | * perfromance and memory usage. Note, though that this should only be used if this |
| 9983 | * view will always be drawn on top of a solid color. |
| 9984 | * |
| 9985 | * @param color The background color to use for the drawing cache's bitmap |
| 9986 | * |
| 9987 | * @see #setDrawingCacheEnabled(boolean) |
| 9988 | * @see #buildDrawingCache() |
| 9989 | * @see #getDrawingCache() |
| 9990 | */ |
| 9991 | public void setDrawingCacheBackgroundColor(int color) { |
Romain Guy | 52e2ef8 | 2010-01-14 12:11:48 -0800 | [diff] [blame] | 9992 | if (color != mDrawingCacheBackgroundColor) { |
| 9993 | mDrawingCacheBackgroundColor = color; |
| 9994 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
| 9995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9996 | } |
| 9997 | |
| 9998 | /** |
| 9999 | * @see #setDrawingCacheBackgroundColor(int) |
| 10000 | * |
| 10001 | * @return The background color to used for the drawing cache's bitmap |
| 10002 | */ |
| 10003 | public int getDrawingCacheBackgroundColor() { |
| 10004 | return mDrawingCacheBackgroundColor; |
| 10005 | } |
| 10006 | |
| 10007 | /** |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10008 | * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10009 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10010 | * @see #buildDrawingCache(boolean) |
| 10011 | */ |
| 10012 | public void buildDrawingCache() { |
| 10013 | buildDrawingCache(false); |
| 10014 | } |
Gilles Debunne | 2ed2eac | 2011-02-24 16:29:48 -0800 | [diff] [blame] | 10015 | |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10016 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10017 | * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p> |
| 10018 | * |
| 10019 | * <p>If you call {@link #buildDrawingCache()} manually without calling |
| 10020 | * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you |
| 10021 | * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10022 | * |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10023 | * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled, |
| 10024 | * this method will create a bitmap of the same size as this view. Because this bitmap |
| 10025 | * will be drawn scaled by the parent ViewGroup, the result on screen might show |
| 10026 | * scaling artifacts. To avoid such artifacts, you should call this method by setting |
| 10027 | * the auto scaling to true. Doing so, however, will generate a bitmap of a different |
| 10028 | * size than the view. This implies that your application must be able to handle this |
| 10029 | * size.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10030 | * |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 10031 | * <p>You should avoid calling this method when hardware acceleration is enabled. If |
| 10032 | * you do not need the drawing cache bitmap, calling this method will increase memory |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10033 | * usage and cause the view to be rendered in software once, thus negatively impacting |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 10034 | * performance.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10035 | * |
| 10036 | * @see #getDrawingCache() |
| 10037 | * @see #destroyDrawingCache() |
| 10038 | */ |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10039 | public void buildDrawingCache(boolean autoScale) { |
| 10040 | if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ? |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 10041 | mDrawingCache == null : mUnscaledDrawingCache == null)) { |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 10042 | mCachingFailed = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10043 | |
| 10044 | if (ViewDebug.TRACE_HIERARCHY) { |
| 10045 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE); |
| 10046 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10047 | |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 10048 | int width = mRight - mLeft; |
| 10049 | int height = mBottom - mTop; |
| 10050 | |
| 10051 | final AttachInfo attachInfo = mAttachInfo; |
Romain Guy | e112322 | 2009-06-29 14:24:56 -0700 | [diff] [blame] | 10052 | final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired; |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10053 | |
Romain Guy | e112322 | 2009-06-29 14:24:56 -0700 | [diff] [blame] | 10054 | if (autoScale && scalingRequired) { |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10055 | width = (int) ((width * attachInfo.mApplicationScale) + 0.5f); |
| 10056 | height = (int) ((height * attachInfo.mApplicationScale) + 0.5f); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 10057 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10058 | |
| 10059 | final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor; |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 10060 | final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque(); |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 10061 | final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10062 | |
| 10063 | if (width <= 0 || height <= 0 || |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 10064 | // Projected bitmap size in bytes |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 10065 | (width * height * (opaque && !use32BitCache ? 2 : 4) > |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10066 | ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) { |
| 10067 | destroyDrawingCache(); |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 10068 | mCachingFailed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10069 | return; |
| 10070 | } |
| 10071 | |
| 10072 | boolean clear = true; |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 10073 | Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10074 | |
| 10075 | if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10076 | Bitmap.Config quality; |
| 10077 | if (!opaque) { |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 10078 | // Never pick ARGB_4444 because it looks awful |
| 10079 | // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10080 | switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) { |
| 10081 | case DRAWING_CACHE_QUALITY_AUTO: |
| 10082 | quality = Bitmap.Config.ARGB_8888; |
| 10083 | break; |
| 10084 | case DRAWING_CACHE_QUALITY_LOW: |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 10085 | quality = Bitmap.Config.ARGB_8888; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10086 | break; |
| 10087 | case DRAWING_CACHE_QUALITY_HIGH: |
| 10088 | quality = Bitmap.Config.ARGB_8888; |
| 10089 | break; |
| 10090 | default: |
| 10091 | quality = Bitmap.Config.ARGB_8888; |
| 10092 | break; |
| 10093 | } |
| 10094 | } else { |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 10095 | // Optimization for translucent windows |
| 10096 | // If the window is translucent, use a 32 bits bitmap to benefit from memcpy() |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 10097 | quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10098 | } |
| 10099 | |
| 10100 | // Try to cleanup memory |
| 10101 | if (bitmap != null) bitmap.recycle(); |
| 10102 | |
| 10103 | try { |
| 10104 | bitmap = Bitmap.createBitmap(width, height, quality); |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 10105 | bitmap.setDensity(getResources().getDisplayMetrics().densityDpi); |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10106 | if (autoScale) { |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 10107 | mDrawingCache = bitmap; |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10108 | } else { |
Romain Guy | 02890fd | 2010-08-06 17:58:44 -0700 | [diff] [blame] | 10109 | mUnscaledDrawingCache = bitmap; |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10110 | } |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 10111 | if (opaque && use32BitCache) bitmap.setHasAlpha(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10112 | } catch (OutOfMemoryError e) { |
| 10113 | // If there is not enough memory to create the bitmap cache, just |
| 10114 | // ignore the issue as bitmap caches are not required to draw the |
| 10115 | // view hierarchy |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10116 | if (autoScale) { |
| 10117 | mDrawingCache = null; |
| 10118 | } else { |
| 10119 | mUnscaledDrawingCache = null; |
| 10120 | } |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 10121 | mCachingFailed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10122 | return; |
| 10123 | } |
| 10124 | |
| 10125 | clear = drawingCacheBackgroundColor != 0; |
| 10126 | } |
| 10127 | |
| 10128 | Canvas canvas; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10129 | if (attachInfo != null) { |
| 10130 | canvas = attachInfo.mCanvas; |
| 10131 | if (canvas == null) { |
| 10132 | canvas = new Canvas(); |
| 10133 | } |
| 10134 | canvas.setBitmap(bitmap); |
| 10135 | // Temporarily clobber the cached Canvas in case one of our children |
| 10136 | // is also using a drawing cache. Without this, the children would |
| 10137 | // steal the canvas by attaching their own bitmap to it and bad, bad |
| 10138 | // thing would happen (invisible views, corrupted drawings, etc.) |
| 10139 | attachInfo.mCanvas = null; |
| 10140 | } else { |
| 10141 | // This case should hopefully never or seldom happen |
| 10142 | canvas = new Canvas(bitmap); |
| 10143 | } |
| 10144 | |
| 10145 | if (clear) { |
| 10146 | bitmap.eraseColor(drawingCacheBackgroundColor); |
| 10147 | } |
| 10148 | |
| 10149 | computeScroll(); |
| 10150 | final int restoreCount = canvas.save(); |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10151 | |
Romain Guy | e112322 | 2009-06-29 14:24:56 -0700 | [diff] [blame] | 10152 | if (autoScale && scalingRequired) { |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 10153 | final float scale = attachInfo.mApplicationScale; |
| 10154 | canvas.scale(scale, scale); |
| 10155 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10157 | canvas.translate(-mScrollX, -mScrollY); |
| 10158 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10159 | mPrivateFlags |= DRAWN; |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 10160 | if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated || |
| 10161 | mLayerType != LAYER_TYPE_NONE) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 10162 | mPrivateFlags |= DRAWING_CACHE_VALID; |
| 10163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10164 | |
| 10165 | // Fast path for layouts with no backgrounds |
| 10166 | if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) { |
| 10167 | if (ViewDebug.TRACE_HIERARCHY) { |
| 10168 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW); |
| 10169 | } |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10170 | mPrivateFlags &= ~DIRTY_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10171 | dispatchDraw(canvas); |
| 10172 | } else { |
| 10173 | draw(canvas); |
| 10174 | } |
| 10175 | |
| 10176 | canvas.restoreToCount(restoreCount); |
| 10177 | |
| 10178 | if (attachInfo != null) { |
| 10179 | // Restore the cached Canvas for our siblings |
| 10180 | attachInfo.mCanvas = canvas; |
| 10181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10182 | } |
| 10183 | } |
| 10184 | |
| 10185 | /** |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10186 | * Create a snapshot of the view into a bitmap. We should probably make |
| 10187 | * some form of this public, but should think about the API. |
| 10188 | */ |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 10189 | Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 10190 | int width = mRight - mLeft; |
| 10191 | int height = mBottom - mTop; |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10192 | |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 10193 | final AttachInfo attachInfo = mAttachInfo; |
Romain Guy | 8c11e31 | 2009-09-14 15:15:30 -0700 | [diff] [blame] | 10194 | final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f; |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 10195 | width = (int) ((width * scale) + 0.5f); |
| 10196 | height = (int) ((height * scale) + 0.5f); |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10197 | |
Romain Guy | 8c11e31 | 2009-09-14 15:15:30 -0700 | [diff] [blame] | 10198 | Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality); |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10199 | if (bitmap == null) { |
| 10200 | throw new OutOfMemoryError(); |
| 10201 | } |
| 10202 | |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 10203 | bitmap.setDensity(getResources().getDisplayMetrics().densityDpi); |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10204 | |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10205 | Canvas canvas; |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10206 | if (attachInfo != null) { |
| 10207 | canvas = attachInfo.mCanvas; |
| 10208 | if (canvas == null) { |
| 10209 | canvas = new Canvas(); |
| 10210 | } |
| 10211 | canvas.setBitmap(bitmap); |
| 10212 | // Temporarily clobber the cached Canvas in case one of our children |
| 10213 | // is also using a drawing cache. Without this, the children would |
| 10214 | // steal the canvas by attaching their own bitmap to it and bad, bad |
| 10215 | // things would happen (invisible views, corrupted drawings, etc.) |
| 10216 | attachInfo.mCanvas = null; |
| 10217 | } else { |
| 10218 | // This case should hopefully never or seldom happen |
| 10219 | canvas = new Canvas(bitmap); |
| 10220 | } |
| 10221 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10222 | if ((backgroundColor & 0xff000000) != 0) { |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10223 | bitmap.eraseColor(backgroundColor); |
| 10224 | } |
| 10225 | |
| 10226 | computeScroll(); |
| 10227 | final int restoreCount = canvas.save(); |
Dianne Hackborn | 8cae124 | 2009-09-10 14:32:16 -0700 | [diff] [blame] | 10228 | canvas.scale(scale, scale); |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10229 | canvas.translate(-mScrollX, -mScrollY); |
| 10230 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10231 | // Temporarily remove the dirty mask |
| 10232 | int flags = mPrivateFlags; |
| 10233 | mPrivateFlags &= ~DIRTY_MASK; |
| 10234 | |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10235 | // Fast path for layouts with no backgrounds |
| 10236 | if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) { |
| 10237 | dispatchDraw(canvas); |
| 10238 | } else { |
| 10239 | draw(canvas); |
| 10240 | } |
| 10241 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10242 | mPrivateFlags = flags; |
| 10243 | |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10244 | canvas.restoreToCount(restoreCount); |
| 10245 | |
| 10246 | if (attachInfo != null) { |
| 10247 | // Restore the cached Canvas for our siblings |
| 10248 | attachInfo.mCanvas = canvas; |
| 10249 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 10250 | |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 10251 | return bitmap; |
| 10252 | } |
| 10253 | |
| 10254 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10255 | * Indicates whether this View is currently in edit mode. A View is usually |
| 10256 | * in edit mode when displayed within a developer tool. For instance, if |
| 10257 | * this View is being drawn by a visual user interface builder, this method |
| 10258 | * should return true. |
| 10259 | * |
| 10260 | * Subclasses should check the return value of this method to provide |
| 10261 | * different behaviors if their normal behavior might interfere with the |
| 10262 | * host environment. For instance: the class spawns a thread in its |
| 10263 | * constructor, the drawing code relies on device-specific features, etc. |
| 10264 | * |
| 10265 | * This method is usually checked in the drawing code of custom widgets. |
| 10266 | * |
| 10267 | * @return True if this View is in edit mode, false otherwise. |
| 10268 | */ |
| 10269 | public boolean isInEditMode() { |
| 10270 | return false; |
| 10271 | } |
| 10272 | |
| 10273 | /** |
| 10274 | * If the View draws content inside its padding and enables fading edges, |
| 10275 | * it needs to support padding offsets. Padding offsets are added to the |
| 10276 | * fading edges to extend the length of the fade so that it covers pixels |
| 10277 | * drawn inside the padding. |
| 10278 | * |
| 10279 | * Subclasses of this class should override this method if they need |
| 10280 | * to draw content inside the padding. |
| 10281 | * |
| 10282 | * @return True if padding offset must be applied, false otherwise. |
| 10283 | * |
| 10284 | * @see #getLeftPaddingOffset() |
| 10285 | * @see #getRightPaddingOffset() |
| 10286 | * @see #getTopPaddingOffset() |
| 10287 | * @see #getBottomPaddingOffset() |
| 10288 | * |
| 10289 | * @since CURRENT |
| 10290 | */ |
| 10291 | protected boolean isPaddingOffsetRequired() { |
| 10292 | return false; |
| 10293 | } |
| 10294 | |
| 10295 | /** |
| 10296 | * Amount by which to extend the left fading region. Called only when |
| 10297 | * {@link #isPaddingOffsetRequired()} returns true. |
| 10298 | * |
| 10299 | * @return The left padding offset in pixels. |
| 10300 | * |
| 10301 | * @see #isPaddingOffsetRequired() |
| 10302 | * |
| 10303 | * @since CURRENT |
| 10304 | */ |
| 10305 | protected int getLeftPaddingOffset() { |
| 10306 | return 0; |
| 10307 | } |
| 10308 | |
| 10309 | /** |
| 10310 | * Amount by which to extend the right fading region. Called only when |
| 10311 | * {@link #isPaddingOffsetRequired()} returns true. |
| 10312 | * |
| 10313 | * @return The right padding offset in pixels. |
| 10314 | * |
| 10315 | * @see #isPaddingOffsetRequired() |
| 10316 | * |
| 10317 | * @since CURRENT |
| 10318 | */ |
| 10319 | protected int getRightPaddingOffset() { |
| 10320 | return 0; |
| 10321 | } |
| 10322 | |
| 10323 | /** |
| 10324 | * Amount by which to extend the top fading region. Called only when |
| 10325 | * {@link #isPaddingOffsetRequired()} returns true. |
| 10326 | * |
| 10327 | * @return The top padding offset in pixels. |
| 10328 | * |
| 10329 | * @see #isPaddingOffsetRequired() |
| 10330 | * |
| 10331 | * @since CURRENT |
| 10332 | */ |
| 10333 | protected int getTopPaddingOffset() { |
| 10334 | return 0; |
| 10335 | } |
| 10336 | |
| 10337 | /** |
| 10338 | * Amount by which to extend the bottom fading region. Called only when |
| 10339 | * {@link #isPaddingOffsetRequired()} returns true. |
| 10340 | * |
| 10341 | * @return The bottom padding offset in pixels. |
| 10342 | * |
| 10343 | * @see #isPaddingOffsetRequired() |
| 10344 | * |
| 10345 | * @since CURRENT |
| 10346 | */ |
| 10347 | protected int getBottomPaddingOffset() { |
| 10348 | return 0; |
| 10349 | } |
| 10350 | |
| 10351 | /** |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 10352 | * @hide |
| 10353 | * @param offsetRequired |
| 10354 | */ |
| 10355 | protected int getFadeTop(boolean offsetRequired) { |
| 10356 | int top = mPaddingTop; |
| 10357 | if (offsetRequired) top += getTopPaddingOffset(); |
| 10358 | return top; |
| 10359 | } |
| 10360 | |
| 10361 | /** |
| 10362 | * @hide |
| 10363 | * @param offsetRequired |
| 10364 | */ |
| 10365 | protected int getFadeHeight(boolean offsetRequired) { |
| 10366 | int padding = mPaddingTop; |
| 10367 | if (offsetRequired) padding += getTopPaddingOffset(); |
| 10368 | return mBottom - mTop - mPaddingBottom - padding; |
| 10369 | } |
| 10370 | |
| 10371 | /** |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 10372 | * <p>Indicates whether this view is attached to an hardware accelerated |
| 10373 | * window or not.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10374 | * |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 10375 | * <p>Even if this method returns true, it does not mean that every call |
| 10376 | * to {@link #draw(android.graphics.Canvas)} will be made with an hardware |
| 10377 | * accelerated {@link android.graphics.Canvas}. For instance, if this view |
| 10378 | * is drawn onto an offscren {@link android.graphics.Bitmap} and its |
| 10379 | * window is hardware accelerated, |
| 10380 | * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely |
| 10381 | * return false, and this method will return true.</p> |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10382 | * |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 10383 | * @return True if the view is attached to a window and the window is |
| 10384 | * hardware accelerated; false in any other case. |
| 10385 | */ |
| 10386 | public boolean isHardwareAccelerated() { |
| 10387 | return mAttachInfo != null && mAttachInfo.mHardwareAccelerated; |
| 10388 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 10389 | |
Romain Guy | 2bffd26 | 2010-09-12 17:40:02 -0700 | [diff] [blame] | 10390 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10391 | * Manually render this view (and all of its children) to the given Canvas. |
| 10392 | * The view must have already done a full layout before this function is |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 10393 | * called. When implementing a view, implement |
| 10394 | * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method. |
| 10395 | * If you do need to override this method, call the superclass version. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10396 | * |
| 10397 | * @param canvas The Canvas to which the View is rendered. |
| 10398 | */ |
| 10399 | public void draw(Canvas canvas) { |
| 10400 | if (ViewDebug.TRACE_HIERARCHY) { |
| 10401 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW); |
| 10402 | } |
| 10403 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 10404 | final int privateFlags = mPrivateFlags; |
| 10405 | final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE && |
| 10406 | (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState); |
| 10407 | mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10409 | /* |
| 10410 | * Draw traversal performs several drawing steps which must be executed |
| 10411 | * in the appropriate order: |
| 10412 | * |
| 10413 | * 1. Draw the background |
| 10414 | * 2. If necessary, save the canvas' layers to prepare for fading |
| 10415 | * 3. Draw view's content |
| 10416 | * 4. Draw children |
| 10417 | * 5. If necessary, draw the fading edges and restore layers |
| 10418 | * 6. Draw decorations (scrollbars for instance) |
| 10419 | */ |
| 10420 | |
| 10421 | // Step 1, draw the background, if needed |
| 10422 | int saveCount; |
| 10423 | |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10424 | if (!dirtyOpaque) { |
| 10425 | final Drawable background = mBGDrawable; |
| 10426 | if (background != null) { |
| 10427 | final int scrollX = mScrollX; |
| 10428 | final int scrollY = mScrollY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10429 | |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10430 | if (mBackgroundSizeChanged) { |
| 10431 | background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); |
| 10432 | mBackgroundSizeChanged = false; |
| 10433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10434 | |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10435 | if ((scrollX | scrollY) == 0) { |
| 10436 | background.draw(canvas); |
| 10437 | } else { |
| 10438 | canvas.translate(scrollX, scrollY); |
| 10439 | background.draw(canvas); |
| 10440 | canvas.translate(-scrollX, -scrollY); |
| 10441 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10442 | } |
| 10443 | } |
| 10444 | |
| 10445 | // skip step 2 & 5 if possible (common case) |
| 10446 | final int viewFlags = mViewFlags; |
| 10447 | boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0; |
| 10448 | boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0; |
| 10449 | if (!verticalEdges && !horizontalEdges) { |
| 10450 | // Step 3, draw the content |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10451 | if (!dirtyOpaque) onDraw(canvas); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10452 | |
| 10453 | // Step 4, draw the children |
| 10454 | dispatchDraw(canvas); |
| 10455 | |
| 10456 | // Step 6, draw decorations (scrollbars) |
| 10457 | onDrawScrollBars(canvas); |
| 10458 | |
| 10459 | // we're done... |
| 10460 | return; |
| 10461 | } |
| 10462 | |
| 10463 | /* |
| 10464 | * Here we do the full fledged routine... |
| 10465 | * (this is an uncommon case where speed matters less, |
| 10466 | * this is why we repeat some of the tests that have been |
| 10467 | * done above) |
| 10468 | */ |
| 10469 | |
| 10470 | boolean drawTop = false; |
| 10471 | boolean drawBottom = false; |
| 10472 | boolean drawLeft = false; |
| 10473 | boolean drawRight = false; |
| 10474 | |
| 10475 | float topFadeStrength = 0.0f; |
| 10476 | float bottomFadeStrength = 0.0f; |
| 10477 | float leftFadeStrength = 0.0f; |
| 10478 | float rightFadeStrength = 0.0f; |
| 10479 | |
| 10480 | // Step 2, save the canvas' layers |
| 10481 | int paddingLeft = mPaddingLeft; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10482 | |
| 10483 | final boolean offsetRequired = isPaddingOffsetRequired(); |
| 10484 | if (offsetRequired) { |
| 10485 | paddingLeft += getLeftPaddingOffset(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10486 | } |
| 10487 | |
| 10488 | int left = mScrollX + paddingLeft; |
| 10489 | int right = left + mRight - mLeft - mPaddingRight - paddingLeft; |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 10490 | int top = mScrollY + getFadeTop(offsetRequired); |
| 10491 | int bottom = top + getFadeHeight(offsetRequired); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10492 | |
| 10493 | if (offsetRequired) { |
| 10494 | right += getRightPaddingOffset(); |
| 10495 | bottom += getBottomPaddingOffset(); |
| 10496 | } |
| 10497 | |
| 10498 | final ScrollabilityCache scrollabilityCache = mScrollCache; |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10499 | final float fadeHeight = scrollabilityCache.fadingEdgeLength; |
| 10500 | int length = (int) fadeHeight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10501 | |
| 10502 | // clip the fade length if top and bottom fades overlap |
| 10503 | // overlapping fades produce odd-looking artifacts |
| 10504 | if (verticalEdges && (top + length > bottom - length)) { |
| 10505 | length = (bottom - top) / 2; |
| 10506 | } |
| 10507 | |
| 10508 | // also clip horizontal fades if necessary |
| 10509 | if (horizontalEdges && (left + length > right - length)) { |
| 10510 | length = (right - left) / 2; |
| 10511 | } |
| 10512 | |
| 10513 | if (verticalEdges) { |
| 10514 | topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength())); |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10515 | drawTop = topFadeStrength * fadeHeight > 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10516 | bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength())); |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10517 | drawBottom = bottomFadeStrength * fadeHeight > 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10518 | } |
| 10519 | |
| 10520 | if (horizontalEdges) { |
| 10521 | leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength())); |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10522 | drawLeft = leftFadeStrength * fadeHeight > 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10523 | rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength())); |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10524 | drawRight = rightFadeStrength * fadeHeight > 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10525 | } |
| 10526 | |
| 10527 | saveCount = canvas.getSaveCount(); |
| 10528 | |
| 10529 | int solidColor = getSolidColor(); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 10530 | if (solidColor == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10531 | final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG; |
| 10532 | |
| 10533 | if (drawTop) { |
| 10534 | canvas.saveLayer(left, top, right, top + length, null, flags); |
| 10535 | } |
| 10536 | |
| 10537 | if (drawBottom) { |
| 10538 | canvas.saveLayer(left, bottom - length, right, bottom, null, flags); |
| 10539 | } |
| 10540 | |
| 10541 | if (drawLeft) { |
| 10542 | canvas.saveLayer(left, top, left + length, bottom, null, flags); |
| 10543 | } |
| 10544 | |
| 10545 | if (drawRight) { |
| 10546 | canvas.saveLayer(right - length, top, right, bottom, null, flags); |
| 10547 | } |
| 10548 | } else { |
| 10549 | scrollabilityCache.setFadeColor(solidColor); |
| 10550 | } |
| 10551 | |
| 10552 | // Step 3, draw the content |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 10553 | if (!dirtyOpaque) onDraw(canvas); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10554 | |
| 10555 | // Step 4, draw the children |
| 10556 | dispatchDraw(canvas); |
| 10557 | |
| 10558 | // Step 5, draw the fade effect and restore layers |
| 10559 | final Paint p = scrollabilityCache.paint; |
| 10560 | final Matrix matrix = scrollabilityCache.matrix; |
| 10561 | final Shader fade = scrollabilityCache.shader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10562 | |
| 10563 | if (drawTop) { |
| 10564 | matrix.setScale(1, fadeHeight * topFadeStrength); |
| 10565 | matrix.postTranslate(left, top); |
| 10566 | fade.setLocalMatrix(matrix); |
| 10567 | canvas.drawRect(left, top, right, top + length, p); |
| 10568 | } |
| 10569 | |
| 10570 | if (drawBottom) { |
| 10571 | matrix.setScale(1, fadeHeight * bottomFadeStrength); |
| 10572 | matrix.postRotate(180); |
| 10573 | matrix.postTranslate(left, bottom); |
| 10574 | fade.setLocalMatrix(matrix); |
| 10575 | canvas.drawRect(left, bottom - length, right, bottom, p); |
| 10576 | } |
| 10577 | |
| 10578 | if (drawLeft) { |
| 10579 | matrix.setScale(1, fadeHeight * leftFadeStrength); |
| 10580 | matrix.postRotate(-90); |
| 10581 | matrix.postTranslate(left, top); |
| 10582 | fade.setLocalMatrix(matrix); |
| 10583 | canvas.drawRect(left, top, left + length, bottom, p); |
| 10584 | } |
| 10585 | |
| 10586 | if (drawRight) { |
| 10587 | matrix.setScale(1, fadeHeight * rightFadeStrength); |
| 10588 | matrix.postRotate(90); |
| 10589 | matrix.postTranslate(right, top); |
| 10590 | fade.setLocalMatrix(matrix); |
| 10591 | canvas.drawRect(right - length, top, right, bottom, p); |
| 10592 | } |
| 10593 | |
| 10594 | canvas.restoreToCount(saveCount); |
| 10595 | |
| 10596 | // Step 6, draw decorations (scrollbars) |
| 10597 | onDrawScrollBars(canvas); |
| 10598 | } |
| 10599 | |
| 10600 | /** |
| 10601 | * Override this if your view is known to always be drawn on top of a solid color background, |
| 10602 | * and needs to draw fading edges. Returning a non-zero color enables the view system to |
| 10603 | * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha |
| 10604 | * should be set to 0xFF. |
| 10605 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 10606 | * @see #setVerticalFadingEdgeEnabled(boolean) |
| 10607 | * @see #setHorizontalFadingEdgeEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10608 | * |
| 10609 | * @return The known solid color background for this view, or 0 if the color may vary |
| 10610 | */ |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 10611 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10612 | public int getSolidColor() { |
| 10613 | return 0; |
| 10614 | } |
| 10615 | |
| 10616 | /** |
| 10617 | * Build a human readable string representation of the specified view flags. |
| 10618 | * |
| 10619 | * @param flags the view flags to convert to a string |
| 10620 | * @return a String representing the supplied flags |
| 10621 | */ |
| 10622 | private static String printFlags(int flags) { |
| 10623 | String output = ""; |
| 10624 | int numFlags = 0; |
| 10625 | if ((flags & FOCUSABLE_MASK) == FOCUSABLE) { |
| 10626 | output += "TAKES_FOCUS"; |
| 10627 | numFlags++; |
| 10628 | } |
| 10629 | |
| 10630 | switch (flags & VISIBILITY_MASK) { |
| 10631 | case INVISIBLE: |
| 10632 | if (numFlags > 0) { |
| 10633 | output += " "; |
| 10634 | } |
| 10635 | output += "INVISIBLE"; |
| 10636 | // USELESS HERE numFlags++; |
| 10637 | break; |
| 10638 | case GONE: |
| 10639 | if (numFlags > 0) { |
| 10640 | output += " "; |
| 10641 | } |
| 10642 | output += "GONE"; |
| 10643 | // USELESS HERE numFlags++; |
| 10644 | break; |
| 10645 | default: |
| 10646 | break; |
| 10647 | } |
| 10648 | return output; |
| 10649 | } |
| 10650 | |
| 10651 | /** |
| 10652 | * Build a human readable string representation of the specified private |
| 10653 | * view flags. |
| 10654 | * |
| 10655 | * @param privateFlags the private view flags to convert to a string |
| 10656 | * @return a String representing the supplied flags |
| 10657 | */ |
| 10658 | private static String printPrivateFlags(int privateFlags) { |
| 10659 | String output = ""; |
| 10660 | int numFlags = 0; |
| 10661 | |
| 10662 | if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) { |
| 10663 | output += "WANTS_FOCUS"; |
| 10664 | numFlags++; |
| 10665 | } |
| 10666 | |
| 10667 | if ((privateFlags & FOCUSED) == FOCUSED) { |
| 10668 | if (numFlags > 0) { |
| 10669 | output += " "; |
| 10670 | } |
| 10671 | output += "FOCUSED"; |
| 10672 | numFlags++; |
| 10673 | } |
| 10674 | |
| 10675 | if ((privateFlags & SELECTED) == SELECTED) { |
| 10676 | if (numFlags > 0) { |
| 10677 | output += " "; |
| 10678 | } |
| 10679 | output += "SELECTED"; |
| 10680 | numFlags++; |
| 10681 | } |
| 10682 | |
| 10683 | if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) { |
| 10684 | if (numFlags > 0) { |
| 10685 | output += " "; |
| 10686 | } |
| 10687 | output += "IS_ROOT_NAMESPACE"; |
| 10688 | numFlags++; |
| 10689 | } |
| 10690 | |
| 10691 | if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) { |
| 10692 | if (numFlags > 0) { |
| 10693 | output += " "; |
| 10694 | } |
| 10695 | output += "HAS_BOUNDS"; |
| 10696 | numFlags++; |
| 10697 | } |
| 10698 | |
| 10699 | if ((privateFlags & DRAWN) == DRAWN) { |
| 10700 | if (numFlags > 0) { |
| 10701 | output += " "; |
| 10702 | } |
| 10703 | output += "DRAWN"; |
| 10704 | // USELESS HERE numFlags++; |
| 10705 | } |
| 10706 | return output; |
| 10707 | } |
| 10708 | |
| 10709 | /** |
| 10710 | * <p>Indicates whether or not this view's layout will be requested during |
| 10711 | * the next hierarchy layout pass.</p> |
| 10712 | * |
| 10713 | * @return true if the layout will be forced during next layout pass |
| 10714 | */ |
| 10715 | public boolean isLayoutRequested() { |
| 10716 | return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT; |
| 10717 | } |
| 10718 | |
| 10719 | /** |
| 10720 | * Assign a size and position to a view and all of its |
| 10721 | * descendants |
| 10722 | * |
| 10723 | * <p>This is the second phase of the layout mechanism. |
| 10724 | * (The first is measuring). In this phase, each parent calls |
| 10725 | * layout on all of its children to position them. |
| 10726 | * This is typically done using the child measurements |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 10727 | * that were stored in the measure pass().</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10728 | * |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 10729 | * <p>Derived classes should not override this method. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10730 | * Derived classes with children should override |
| 10731 | * onLayout. In that method, they should |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 10732 | * call layout on each of their children.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10733 | * |
| 10734 | * @param l Left position, relative to parent |
| 10735 | * @param t Top position, relative to parent |
| 10736 | * @param r Right position, relative to parent |
| 10737 | * @param b Bottom position, relative to parent |
| 10738 | */ |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 10739 | @SuppressWarnings({"unchecked"}) |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 10740 | public void layout(int l, int t, int r, int b) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 10741 | int oldL = mLeft; |
| 10742 | int oldT = mTop; |
| 10743 | int oldB = mBottom; |
| 10744 | int oldR = mRight; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10745 | boolean changed = setFrame(l, t, r, b); |
| 10746 | if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) { |
| 10747 | if (ViewDebug.TRACE_HIERARCHY) { |
| 10748 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT); |
| 10749 | } |
| 10750 | |
| 10751 | onLayout(changed, l, t, r, b); |
| 10752 | mPrivateFlags &= ~LAYOUT_REQUIRED; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 10753 | |
| 10754 | if (mOnLayoutChangeListeners != null) { |
| 10755 | ArrayList<OnLayoutChangeListener> listenersCopy = |
| 10756 | (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone(); |
| 10757 | int numListeners = listenersCopy.size(); |
| 10758 | for (int i = 0; i < numListeners; ++i) { |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 10759 | listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 10760 | } |
| 10761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10762 | } |
| 10763 | mPrivateFlags &= ~FORCE_LAYOUT; |
| 10764 | } |
| 10765 | |
| 10766 | /** |
| 10767 | * Called from layout when this view should |
| 10768 | * assign a size and position to each of its children. |
| 10769 | * |
| 10770 | * Derived classes with children should override |
| 10771 | * this method and call layout on each of |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 10772 | * their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10773 | * @param changed This is a new size or position for this view |
| 10774 | * @param left Left position, relative to parent |
| 10775 | * @param top Top position, relative to parent |
| 10776 | * @param right Right position, relative to parent |
| 10777 | * @param bottom Bottom position, relative to parent |
| 10778 | */ |
| 10779 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 10780 | } |
| 10781 | |
| 10782 | /** |
| 10783 | * Assign a size and position to this view. |
| 10784 | * |
| 10785 | * This is called from layout. |
| 10786 | * |
| 10787 | * @param left Left position, relative to parent |
| 10788 | * @param top Top position, relative to parent |
| 10789 | * @param right Right position, relative to parent |
| 10790 | * @param bottom Bottom position, relative to parent |
| 10791 | * @return true if the new size and position are different than the |
| 10792 | * previous ones |
| 10793 | * {@hide} |
| 10794 | */ |
| 10795 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 10796 | boolean changed = false; |
| 10797 | |
| 10798 | if (DBG) { |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 10799 | Log.d("View", this + " View.setFrame(" + left + "," + top + "," |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10800 | + right + "," + bottom + ")"); |
| 10801 | } |
| 10802 | |
| 10803 | if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { |
| 10804 | changed = true; |
| 10805 | |
| 10806 | // Remember our drawn bit |
| 10807 | int drawn = mPrivateFlags & DRAWN; |
| 10808 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10809 | int oldWidth = mRight - mLeft; |
| 10810 | int oldHeight = mBottom - mTop; |
Chet Haase | 75755e2 | 2011-07-18 17:48:25 -0700 | [diff] [blame] | 10811 | int newWidth = right - left; |
| 10812 | int newHeight = bottom - top; |
| 10813 | boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight); |
| 10814 | |
| 10815 | // Invalidate our old position |
| 10816 | invalidate(sizeChanged); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10817 | |
| 10818 | mLeft = left; |
| 10819 | mTop = top; |
| 10820 | mRight = right; |
| 10821 | mBottom = bottom; |
| 10822 | |
| 10823 | mPrivateFlags |= HAS_BOUNDS; |
| 10824 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10825 | |
Chet Haase | 75755e2 | 2011-07-18 17:48:25 -0700 | [diff] [blame] | 10826 | if (sizeChanged) { |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 10827 | if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) { |
| 10828 | // A change in dimension means an auto-centered pivot point changes, too |
| 10829 | mMatrixDirty = true; |
| 10830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10831 | onSizeChanged(newWidth, newHeight, oldWidth, oldHeight); |
| 10832 | } |
| 10833 | |
| 10834 | if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 10835 | // If we are visible, force the DRAWN bit to on so that |
| 10836 | // this invalidate will go through (at least to our parent). |
| 10837 | // This is because someone may have invalidated this view |
Chet Haase | 6c7ad5d | 2010-12-28 08:40:00 -0800 | [diff] [blame] | 10838 | // before this call to setFrame came in, thereby clearing |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10839 | // the DRAWN bit. |
| 10840 | mPrivateFlags |= DRAWN; |
Chet Haase | 75755e2 | 2011-07-18 17:48:25 -0700 | [diff] [blame] | 10841 | invalidate(sizeChanged); |
Chet Haase | f28595e | 2011-01-31 18:52:12 -0800 | [diff] [blame] | 10842 | // parent display list may need to be recreated based on a change in the bounds |
| 10843 | // of any child |
| 10844 | invalidateParentCaches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10845 | } |
| 10846 | |
| 10847 | // Reset drawn bit to original value (invalidate turns it off) |
| 10848 | mPrivateFlags |= drawn; |
| 10849 | |
| 10850 | mBackgroundSizeChanged = true; |
| 10851 | } |
| 10852 | return changed; |
| 10853 | } |
| 10854 | |
| 10855 | /** |
| 10856 | * Finalize inflating a view from XML. This is called as the last phase |
| 10857 | * of inflation, after all child views have been added. |
| 10858 | * |
| 10859 | * <p>Even if the subclass overrides onFinishInflate, they should always be |
| 10860 | * sure to call the super method, so that we get called. |
| 10861 | */ |
| 10862 | protected void onFinishInflate() { |
| 10863 | } |
| 10864 | |
| 10865 | /** |
| 10866 | * Returns the resources associated with this view. |
| 10867 | * |
| 10868 | * @return Resources object. |
| 10869 | */ |
| 10870 | public Resources getResources() { |
| 10871 | return mResources; |
| 10872 | } |
| 10873 | |
| 10874 | /** |
| 10875 | * Invalidates the specified Drawable. |
| 10876 | * |
| 10877 | * @param drawable the drawable to invalidate |
| 10878 | */ |
| 10879 | public void invalidateDrawable(Drawable drawable) { |
| 10880 | if (verifyDrawable(drawable)) { |
| 10881 | final Rect dirty = drawable.getBounds(); |
| 10882 | final int scrollX = mScrollX; |
| 10883 | final int scrollY = mScrollY; |
| 10884 | |
| 10885 | invalidate(dirty.left + scrollX, dirty.top + scrollY, |
| 10886 | dirty.right + scrollX, dirty.bottom + scrollY); |
| 10887 | } |
| 10888 | } |
| 10889 | |
| 10890 | /** |
| 10891 | * Schedules an action on a drawable to occur at a specified time. |
| 10892 | * |
| 10893 | * @param who the recipient of the action |
| 10894 | * @param what the action to run on the drawable |
| 10895 | * @param when the time at which the action must occur. Uses the |
| 10896 | * {@link SystemClock#uptimeMillis} timebase. |
| 10897 | */ |
| 10898 | public void scheduleDrawable(Drawable who, Runnable what, long when) { |
| 10899 | if (verifyDrawable(who) && what != null && mAttachInfo != null) { |
| 10900 | mAttachInfo.mHandler.postAtTime(what, who, when); |
| 10901 | } |
| 10902 | } |
| 10903 | |
| 10904 | /** |
| 10905 | * Cancels a scheduled action on a drawable. |
| 10906 | * |
| 10907 | * @param who the recipient of the action |
| 10908 | * @param what the action to cancel |
| 10909 | */ |
| 10910 | public void unscheduleDrawable(Drawable who, Runnable what) { |
| 10911 | if (verifyDrawable(who) && what != null && mAttachInfo != null) { |
| 10912 | mAttachInfo.mHandler.removeCallbacks(what, who); |
| 10913 | } |
| 10914 | } |
| 10915 | |
| 10916 | /** |
| 10917 | * Unschedule any events associated with the given Drawable. This can be |
| 10918 | * used when selecting a new Drawable into a view, so that the previous |
| 10919 | * one is completely unscheduled. |
| 10920 | * |
| 10921 | * @param who The Drawable to unschedule. |
| 10922 | * |
| 10923 | * @see #drawableStateChanged |
| 10924 | */ |
| 10925 | public void unscheduleDrawable(Drawable who) { |
| 10926 | if (mAttachInfo != null) { |
| 10927 | mAttachInfo.mHandler.removeCallbacksAndMessages(who); |
| 10928 | } |
| 10929 | } |
| 10930 | |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 10931 | /** |
| 10932 | * Return the layout direction of a given Drawable. |
| 10933 | * |
| 10934 | * @param who the Drawable to query |
| 10935 | * |
| 10936 | * @hide |
| 10937 | */ |
| 10938 | public int getResolvedLayoutDirection(Drawable who) { |
| 10939 | return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 10940 | } |
| 10941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10942 | /** |
| 10943 | * If your view subclass is displaying its own Drawable objects, it should |
| 10944 | * override this function and return true for any Drawable it is |
| 10945 | * displaying. This allows animations for those drawables to be |
| 10946 | * scheduled. |
| 10947 | * |
| 10948 | * <p>Be sure to call through to the super class when overriding this |
| 10949 | * function. |
| 10950 | * |
| 10951 | * @param who The Drawable to verify. Return true if it is one you are |
| 10952 | * displaying, else return the result of calling through to the |
| 10953 | * super class. |
| 10954 | * |
| 10955 | * @return boolean If true than the Drawable is being displayed in the |
| 10956 | * view; else false and it is not allowed to animate. |
| 10957 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 10958 | * @see #unscheduleDrawable(android.graphics.drawable.Drawable) |
| 10959 | * @see #drawableStateChanged() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10960 | */ |
| 10961 | protected boolean verifyDrawable(Drawable who) { |
| 10962 | return who == mBGDrawable; |
| 10963 | } |
| 10964 | |
| 10965 | /** |
| 10966 | * This function is called whenever the state of the view changes in such |
| 10967 | * a way that it impacts the state of drawables being shown. |
| 10968 | * |
| 10969 | * <p>Be sure to call through to the superclass when overriding this |
| 10970 | * function. |
| 10971 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 10972 | * @see Drawable#setState(int[]) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10973 | */ |
| 10974 | protected void drawableStateChanged() { |
| 10975 | Drawable d = mBGDrawable; |
| 10976 | if (d != null && d.isStateful()) { |
| 10977 | d.setState(getDrawableState()); |
| 10978 | } |
| 10979 | } |
| 10980 | |
| 10981 | /** |
| 10982 | * Call this to force a view to update its drawable state. This will cause |
| 10983 | * drawableStateChanged to be called on this view. Views that are interested |
| 10984 | * in the new state should call getDrawableState. |
| 10985 | * |
| 10986 | * @see #drawableStateChanged |
| 10987 | * @see #getDrawableState |
| 10988 | */ |
| 10989 | public void refreshDrawableState() { |
| 10990 | mPrivateFlags |= DRAWABLE_STATE_DIRTY; |
| 10991 | drawableStateChanged(); |
| 10992 | |
| 10993 | ViewParent parent = mParent; |
| 10994 | if (parent != null) { |
| 10995 | parent.childDrawableStateChanged(this); |
| 10996 | } |
| 10997 | } |
| 10998 | |
| 10999 | /** |
| 11000 | * Return an array of resource IDs of the drawable states representing the |
| 11001 | * current state of the view. |
| 11002 | * |
| 11003 | * @return The current drawable state |
| 11004 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11005 | * @see Drawable#setState(int[]) |
| 11006 | * @see #drawableStateChanged() |
| 11007 | * @see #onCreateDrawableState(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11008 | */ |
| 11009 | public final int[] getDrawableState() { |
| 11010 | if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) { |
| 11011 | return mDrawableState; |
| 11012 | } else { |
| 11013 | mDrawableState = onCreateDrawableState(0); |
| 11014 | mPrivateFlags &= ~DRAWABLE_STATE_DIRTY; |
| 11015 | return mDrawableState; |
| 11016 | } |
| 11017 | } |
| 11018 | |
| 11019 | /** |
| 11020 | * Generate the new {@link android.graphics.drawable.Drawable} state for |
| 11021 | * this view. This is called by the view |
| 11022 | * system when the cached Drawable state is determined to be invalid. To |
| 11023 | * retrieve the current state, you should use {@link #getDrawableState}. |
| 11024 | * |
| 11025 | * @param extraSpace if non-zero, this is the number of extra entries you |
| 11026 | * would like in the returned array in which you can place your own |
| 11027 | * states. |
| 11028 | * |
| 11029 | * @return Returns an array holding the current {@link Drawable} state of |
| 11030 | * the view. |
| 11031 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11032 | * @see #mergeDrawableStates(int[], int[]) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11033 | */ |
| 11034 | protected int[] onCreateDrawableState(int extraSpace) { |
| 11035 | if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE && |
| 11036 | mParent instanceof View) { |
| 11037 | return ((View) mParent).onCreateDrawableState(extraSpace); |
| 11038 | } |
| 11039 | |
| 11040 | int[] drawableState; |
| 11041 | |
| 11042 | int privateFlags = mPrivateFlags; |
| 11043 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11044 | int viewStateIndex = 0; |
| 11045 | if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED; |
| 11046 | if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED; |
| 11047 | if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED; |
Neel Parekh | e537858 | 2010-10-06 11:36:50 -0700 | [diff] [blame] | 11048 | if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED; |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11049 | if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED; |
| 11050 | if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED; |
Adam Powell | 5a7e94e | 2011-04-25 15:30:43 -0700 | [diff] [blame] | 11051 | if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested && |
| 11052 | HardwareRenderer.isAvailable()) { |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 11053 | // This is set if HW acceleration is requested, even if the current |
| 11054 | // process doesn't allow it. This is just to allow app preview |
| 11055 | // windows to better match their app. |
| 11056 | viewStateIndex |= VIEW_STATE_ACCELERATED; |
| 11057 | } |
PY Laligand | c33d8d49e | 2011-03-14 18:22:53 -0700 | [diff] [blame] | 11058 | if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11059 | |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 11060 | final int privateFlags2 = mPrivateFlags2; |
| 11061 | if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT; |
| 11062 | if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED; |
| 11063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11064 | drawableState = VIEW_STATE_SETS[viewStateIndex]; |
| 11065 | |
| 11066 | //noinspection ConstantIfStatement |
| 11067 | if (false) { |
| 11068 | Log.i("View", "drawableStateIndex=" + viewStateIndex); |
| 11069 | Log.i("View", toString() |
| 11070 | + " pressed=" + ((privateFlags & PRESSED) != 0) |
| 11071 | + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED) |
| 11072 | + " fo=" + hasFocus() |
| 11073 | + " sl=" + ((privateFlags & SELECTED) != 0) |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11074 | + " wf=" + hasWindowFocus() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11075 | + ": " + Arrays.toString(drawableState)); |
| 11076 | } |
| 11077 | |
| 11078 | if (extraSpace == 0) { |
| 11079 | return drawableState; |
| 11080 | } |
| 11081 | |
| 11082 | final int[] fullState; |
| 11083 | if (drawableState != null) { |
| 11084 | fullState = new int[drawableState.length + extraSpace]; |
| 11085 | System.arraycopy(drawableState, 0, fullState, 0, drawableState.length); |
| 11086 | } else { |
| 11087 | fullState = new int[extraSpace]; |
| 11088 | } |
| 11089 | |
| 11090 | return fullState; |
| 11091 | } |
| 11092 | |
| 11093 | /** |
| 11094 | * Merge your own state values in <var>additionalState</var> into the base |
| 11095 | * state values <var>baseState</var> that were returned by |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11096 | * {@link #onCreateDrawableState(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11097 | * |
| 11098 | * @param baseState The base state values returned by |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11099 | * {@link #onCreateDrawableState(int)}, which will be modified to also hold your |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11100 | * own additional state values. |
| 11101 | * |
| 11102 | * @param additionalState The additional state values you would like |
| 11103 | * added to <var>baseState</var>; this array is not modified. |
| 11104 | * |
| 11105 | * @return As a convenience, the <var>baseState</var> array you originally |
| 11106 | * passed into the function is returned. |
| 11107 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11108 | * @see #onCreateDrawableState(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11109 | */ |
| 11110 | protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) { |
| 11111 | final int N = baseState.length; |
| 11112 | int i = N - 1; |
| 11113 | while (i >= 0 && baseState[i] == 0) { |
| 11114 | i--; |
| 11115 | } |
| 11116 | System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length); |
| 11117 | return baseState; |
| 11118 | } |
| 11119 | |
| 11120 | /** |
Dianne Hackborn | 079e235 | 2010-10-18 17:02:43 -0700 | [diff] [blame] | 11121 | * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()} |
| 11122 | * on all Drawable objects associated with this view. |
| 11123 | */ |
| 11124 | public void jumpDrawablesToCurrentState() { |
| 11125 | if (mBGDrawable != null) { |
| 11126 | mBGDrawable.jumpToCurrentState(); |
| 11127 | } |
| 11128 | } |
| 11129 | |
| 11130 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11131 | * Sets the background color for this view. |
| 11132 | * @param color the color of the background |
| 11133 | */ |
Bjorn Bringert | 8354fa6 | 2010-02-24 23:54:29 +0000 | [diff] [blame] | 11134 | @RemotableViewMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11135 | public void setBackgroundColor(int color) { |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 11136 | if (mBGDrawable instanceof ColorDrawable) { |
| 11137 | ((ColorDrawable) mBGDrawable).setColor(color); |
| 11138 | } else { |
| 11139 | setBackgroundDrawable(new ColorDrawable(color)); |
| 11140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11141 | } |
| 11142 | |
| 11143 | /** |
| 11144 | * Set the background to a given resource. The resource should refer to |
Wink Saville | 7cd88e1 | 2009-08-04 14:45:10 -0700 | [diff] [blame] | 11145 | * a Drawable object or 0 to remove the background. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11146 | * @param resid The identifier of the resource. |
| 11147 | * @attr ref android.R.styleable#View_background |
| 11148 | */ |
Bjorn Bringert | 8354fa6 | 2010-02-24 23:54:29 +0000 | [diff] [blame] | 11149 | @RemotableViewMethod |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11150 | public void setBackgroundResource(int resid) { |
| 11151 | if (resid != 0 && resid == mBackgroundResource) { |
| 11152 | return; |
| 11153 | } |
| 11154 | |
| 11155 | Drawable d= null; |
| 11156 | if (resid != 0) { |
| 11157 | d = mResources.getDrawable(resid); |
| 11158 | } |
| 11159 | setBackgroundDrawable(d); |
| 11160 | |
| 11161 | mBackgroundResource = resid; |
| 11162 | } |
| 11163 | |
| 11164 | /** |
| 11165 | * Set the background to a given Drawable, or remove the background. If the |
| 11166 | * background has padding, this View's padding is set to the background's |
| 11167 | * padding. However, when a background is removed, this View's padding isn't |
| 11168 | * touched. If setting the padding is desired, please use |
| 11169 | * {@link #setPadding(int, int, int, int)}. |
| 11170 | * |
| 11171 | * @param d The Drawable to use as the background, or null to remove the |
| 11172 | * background |
| 11173 | */ |
| 11174 | public void setBackgroundDrawable(Drawable d) { |
Adam Powell | 4d36ec1 | 2011-07-17 16:44:16 -0700 | [diff] [blame] | 11175 | if (d == mBGDrawable) { |
| 11176 | return; |
| 11177 | } |
| 11178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11179 | boolean requestLayout = false; |
| 11180 | |
| 11181 | mBackgroundResource = 0; |
| 11182 | |
| 11183 | /* |
| 11184 | * Regardless of whether we're setting a new background or not, we want |
| 11185 | * to clear the previous drawable. |
| 11186 | */ |
| 11187 | if (mBGDrawable != null) { |
| 11188 | mBGDrawable.setCallback(null); |
| 11189 | unscheduleDrawable(mBGDrawable); |
| 11190 | } |
| 11191 | |
| 11192 | if (d != null) { |
| 11193 | Rect padding = sThreadLocal.get(); |
| 11194 | if (padding == null) { |
| 11195 | padding = new Rect(); |
| 11196 | sThreadLocal.set(padding); |
| 11197 | } |
| 11198 | if (d.getPadding(padding)) { |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11199 | switch (d.getResolvedLayoutDirectionSelf()) { |
| 11200 | case LAYOUT_DIRECTION_RTL: |
| 11201 | setPadding(padding.right, padding.top, padding.left, padding.bottom); |
| 11202 | break; |
| 11203 | case LAYOUT_DIRECTION_LTR: |
| 11204 | default: |
| 11205 | setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 11206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11207 | } |
| 11208 | |
| 11209 | // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or |
| 11210 | // if it has a different minimum size, we should layout again |
| 11211 | if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() || |
| 11212 | mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) { |
| 11213 | requestLayout = true; |
| 11214 | } |
| 11215 | |
| 11216 | d.setCallback(this); |
| 11217 | if (d.isStateful()) { |
| 11218 | d.setState(getDrawableState()); |
| 11219 | } |
| 11220 | d.setVisible(getVisibility() == VISIBLE, false); |
| 11221 | mBGDrawable = d; |
| 11222 | |
| 11223 | if ((mPrivateFlags & SKIP_DRAW) != 0) { |
| 11224 | mPrivateFlags &= ~SKIP_DRAW; |
| 11225 | mPrivateFlags |= ONLY_DRAWS_BACKGROUND; |
| 11226 | requestLayout = true; |
| 11227 | } |
| 11228 | } else { |
| 11229 | /* Remove the background */ |
| 11230 | mBGDrawable = null; |
| 11231 | |
| 11232 | if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) { |
| 11233 | /* |
| 11234 | * This view ONLY drew the background before and we're removing |
| 11235 | * the background, so now it won't draw anything |
| 11236 | * (hence we SKIP_DRAW) |
| 11237 | */ |
| 11238 | mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND; |
| 11239 | mPrivateFlags |= SKIP_DRAW; |
| 11240 | } |
| 11241 | |
| 11242 | /* |
| 11243 | * When the background is set, we try to apply its padding to this |
| 11244 | * View. When the background is removed, we don't touch this View's |
| 11245 | * padding. This is noted in the Javadocs. Hence, we don't need to |
| 11246 | * requestLayout(), the invalidate() below is sufficient. |
| 11247 | */ |
| 11248 | |
| 11249 | // The old background's minimum size could have affected this |
| 11250 | // View's layout, so let's requestLayout |
| 11251 | requestLayout = true; |
| 11252 | } |
| 11253 | |
Romain Guy | 8f1344f5 | 2009-05-15 16:03:59 -0700 | [diff] [blame] | 11254 | computeOpaqueFlags(); |
| 11255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11256 | if (requestLayout) { |
| 11257 | requestLayout(); |
| 11258 | } |
| 11259 | |
| 11260 | mBackgroundSizeChanged = true; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 11261 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11262 | } |
| 11263 | |
| 11264 | /** |
| 11265 | * Gets the background drawable |
| 11266 | * @return The drawable used as the background for this view, if any. |
| 11267 | */ |
| 11268 | public Drawable getBackground() { |
| 11269 | return mBGDrawable; |
| 11270 | } |
| 11271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11272 | /** |
| 11273 | * Sets the padding. The view may add on the space required to display |
| 11274 | * the scrollbars, depending on the style and visibility of the scrollbars. |
| 11275 | * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop}, |
| 11276 | * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different |
| 11277 | * from the values set in this call. |
| 11278 | * |
| 11279 | * @attr ref android.R.styleable#View_padding |
| 11280 | * @attr ref android.R.styleable#View_paddingBottom |
| 11281 | * @attr ref android.R.styleable#View_paddingLeft |
| 11282 | * @attr ref android.R.styleable#View_paddingRight |
| 11283 | * @attr ref android.R.styleable#View_paddingTop |
| 11284 | * @param left the left padding in pixels |
| 11285 | * @param top the top padding in pixels |
| 11286 | * @param right the right padding in pixels |
| 11287 | * @param bottom the bottom padding in pixels |
| 11288 | */ |
| 11289 | public void setPadding(int left, int top, int right, int bottom) { |
| 11290 | boolean changed = false; |
| 11291 | |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11292 | mUserPaddingRelative = false; |
| 11293 | |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 11294 | mUserPaddingLeft = left; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11295 | mUserPaddingRight = right; |
| 11296 | mUserPaddingBottom = bottom; |
| 11297 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11298 | final int viewFlags = mViewFlags; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11299 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11300 | // Common case is there are no scroll bars. |
| 11301 | if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11302 | if ((viewFlags & SCROLLBARS_VERTICAL) != 0) { |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 11303 | final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0 |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11304 | ? 0 : getVerticalScrollbarWidth(); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 11305 | switch (mVerticalScrollbarPosition) { |
| 11306 | case SCROLLBAR_POSITION_DEFAULT: |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11307 | if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) { |
| 11308 | left += offset; |
| 11309 | } else { |
| 11310 | right += offset; |
| 11311 | } |
| 11312 | break; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 11313 | case SCROLLBAR_POSITION_RIGHT: |
| 11314 | right += offset; |
| 11315 | break; |
| 11316 | case SCROLLBAR_POSITION_LEFT: |
| 11317 | left += offset; |
| 11318 | break; |
| 11319 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11320 | } |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 11321 | if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11322 | bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0 |
| 11323 | ? 0 : getHorizontalScrollbarHeight(); |
| 11324 | } |
| 11325 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11326 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11327 | if (mPaddingLeft != left) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11328 | changed = true; |
| 11329 | mPaddingLeft = left; |
| 11330 | } |
| 11331 | if (mPaddingTop != top) { |
| 11332 | changed = true; |
| 11333 | mPaddingTop = top; |
| 11334 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11335 | if (mPaddingRight != right) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11336 | changed = true; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11337 | mPaddingRight = right; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11338 | } |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11339 | if (mPaddingBottom != bottom) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11340 | changed = true; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 11341 | mPaddingBottom = bottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11342 | } |
| 11343 | |
| 11344 | if (changed) { |
| 11345 | requestLayout(); |
| 11346 | } |
| 11347 | } |
| 11348 | |
| 11349 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11350 | * Sets the relative padding. The view may add on the space required to display |
| 11351 | * the scrollbars, depending on the style and visibility of the scrollbars. |
| 11352 | * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop}, |
| 11353 | * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different |
| 11354 | * from the values set in this call. |
| 11355 | * |
| 11356 | * @attr ref android.R.styleable#View_padding |
| 11357 | * @attr ref android.R.styleable#View_paddingBottom |
| 11358 | * @attr ref android.R.styleable#View_paddingStart |
| 11359 | * @attr ref android.R.styleable#View_paddingEnd |
| 11360 | * @attr ref android.R.styleable#View_paddingTop |
| 11361 | * @param start the start padding in pixels |
| 11362 | * @param top the top padding in pixels |
| 11363 | * @param end the end padding in pixels |
| 11364 | * @param bottom the bottom padding in pixels |
| 11365 | * |
| 11366 | * @hide |
| 11367 | */ |
| 11368 | public void setPaddingRelative(int start, int top, int end, int bottom) { |
| 11369 | mUserPaddingRelative = true; |
Fabrice Di Meglio | f9e3650 | 2011-06-21 18:41:48 -0700 | [diff] [blame] | 11370 | |
| 11371 | mUserPaddingStart = start; |
| 11372 | mUserPaddingEnd = end; |
| 11373 | |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11374 | switch(getResolvedLayoutDirection()) { |
| 11375 | case LAYOUT_DIRECTION_RTL: |
| 11376 | setPadding(end, top, start, bottom); |
| 11377 | break; |
| 11378 | case LAYOUT_DIRECTION_LTR: |
| 11379 | default: |
| 11380 | setPadding(start, top, end, bottom); |
| 11381 | } |
| 11382 | } |
| 11383 | |
| 11384 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11385 | * Returns the top padding of this view. |
| 11386 | * |
| 11387 | * @return the top padding in pixels |
| 11388 | */ |
| 11389 | public int getPaddingTop() { |
| 11390 | return mPaddingTop; |
| 11391 | } |
| 11392 | |
| 11393 | /** |
| 11394 | * Returns the bottom padding of this view. If there are inset and enabled |
| 11395 | * scrollbars, this value may include the space required to display the |
| 11396 | * scrollbars as well. |
| 11397 | * |
| 11398 | * @return the bottom padding in pixels |
| 11399 | */ |
| 11400 | public int getPaddingBottom() { |
| 11401 | return mPaddingBottom; |
| 11402 | } |
| 11403 | |
| 11404 | /** |
| 11405 | * Returns the left padding of this view. If there are inset and enabled |
| 11406 | * scrollbars, this value may include the space required to display the |
| 11407 | * scrollbars as well. |
| 11408 | * |
| 11409 | * @return the left padding in pixels |
| 11410 | */ |
| 11411 | public int getPaddingLeft() { |
| 11412 | return mPaddingLeft; |
| 11413 | } |
| 11414 | |
| 11415 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11416 | * Returns the start padding of this view. If there are inset and enabled |
| 11417 | * scrollbars, this value may include the space required to display the |
| 11418 | * scrollbars as well. |
| 11419 | * |
| 11420 | * @return the start padding in pixels |
| 11421 | * |
| 11422 | * @hide |
| 11423 | */ |
| 11424 | public int getPaddingStart() { |
| 11425 | return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? |
| 11426 | mPaddingRight : mPaddingLeft; |
| 11427 | } |
| 11428 | |
| 11429 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11430 | * Returns the right padding of this view. If there are inset and enabled |
| 11431 | * scrollbars, this value may include the space required to display the |
| 11432 | * scrollbars as well. |
| 11433 | * |
| 11434 | * @return the right padding in pixels |
| 11435 | */ |
| 11436 | public int getPaddingRight() { |
| 11437 | return mPaddingRight; |
| 11438 | } |
| 11439 | |
| 11440 | /** |
Fabrice Di Meglio | d8703a9 | 2011-06-16 18:54:08 -0700 | [diff] [blame] | 11441 | * Returns the end padding of this view. If there are inset and enabled |
| 11442 | * scrollbars, this value may include the space required to display the |
| 11443 | * scrollbars as well. |
| 11444 | * |
| 11445 | * @return the end padding in pixels |
| 11446 | * |
| 11447 | * @hide |
| 11448 | */ |
| 11449 | public int getPaddingEnd() { |
| 11450 | return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ? |
| 11451 | mPaddingLeft : mPaddingRight; |
| 11452 | } |
| 11453 | |
| 11454 | /** |
| 11455 | * Return if the padding as been set thru relative values |
| 11456 | * {@link #setPaddingRelative(int, int, int, int)} or thru |
| 11457 | * @attr ref android.R.styleable#View_paddingStart or |
| 11458 | * @attr ref android.R.styleable#View_paddingEnd |
| 11459 | * |
| 11460 | * @return true if the padding is relative or false if it is not. |
| 11461 | * |
| 11462 | * @hide |
| 11463 | */ |
| 11464 | public boolean isPaddingRelative() { |
| 11465 | return mUserPaddingRelative; |
| 11466 | } |
| 11467 | |
| 11468 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11469 | * Changes the selection state of this view. A view can be selected or not. |
| 11470 | * Note that selection is not the same as focus. Views are typically |
| 11471 | * selected in the context of an AdapterView like ListView or GridView; |
| 11472 | * the selected view is the view that is highlighted. |
| 11473 | * |
| 11474 | * @param selected true if the view must be selected, false otherwise |
| 11475 | */ |
| 11476 | public void setSelected(boolean selected) { |
| 11477 | if (((mPrivateFlags & SELECTED) != 0) != selected) { |
| 11478 | mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0); |
Romain Guy | a2431d0 | 2009-04-30 16:30:00 -0700 | [diff] [blame] | 11479 | if (!selected) resetPressedState(); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 11480 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11481 | refreshDrawableState(); |
| 11482 | dispatchSetSelected(selected); |
| 11483 | } |
| 11484 | } |
| 11485 | |
| 11486 | /** |
| 11487 | * Dispatch setSelected to all of this View's children. |
| 11488 | * |
| 11489 | * @see #setSelected(boolean) |
| 11490 | * |
| 11491 | * @param selected The new selected state |
| 11492 | */ |
| 11493 | protected void dispatchSetSelected(boolean selected) { |
| 11494 | } |
| 11495 | |
| 11496 | /** |
| 11497 | * Indicates the selection state of this view. |
| 11498 | * |
| 11499 | * @return true if the view is selected, false otherwise |
| 11500 | */ |
| 11501 | @ViewDebug.ExportedProperty |
| 11502 | public boolean isSelected() { |
| 11503 | return (mPrivateFlags & SELECTED) != 0; |
| 11504 | } |
| 11505 | |
| 11506 | /** |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11507 | * Changes the activated state of this view. A view can be activated or not. |
| 11508 | * Note that activation is not the same as selection. Selection is |
| 11509 | * a transient property, representing the view (hierarchy) the user is |
| 11510 | * currently interacting with. Activation is a longer-term state that the |
| 11511 | * user can move views in and out of. For example, in a list view with |
| 11512 | * single or multiple selection enabled, the views in the current selection |
| 11513 | * set are activated. (Um, yeah, we are deeply sorry about the terminology |
| 11514 | * here.) The activated state is propagated down to children of the view it |
| 11515 | * is set on. |
| 11516 | * |
| 11517 | * @param activated true if the view must be activated, false otherwise |
| 11518 | */ |
| 11519 | public void setActivated(boolean activated) { |
| 11520 | if (((mPrivateFlags & ACTIVATED) != 0) != activated) { |
| 11521 | mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 11522 | invalidate(true); |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11523 | refreshDrawableState(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 11524 | dispatchSetActivated(activated); |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 11525 | } |
| 11526 | } |
| 11527 | |
| 11528 | /** |
| 11529 | * Dispatch setActivated to all of this View's children. |
| 11530 | * |
| 11531 | * @see #setActivated(boolean) |
| 11532 | * |
| 11533 | * @param activated The new activated state |
| 11534 | */ |
| 11535 | protected void dispatchSetActivated(boolean activated) { |
| 11536 | } |
| 11537 | |
| 11538 | /** |
| 11539 | * Indicates the activation state of this view. |
| 11540 | * |
| 11541 | * @return true if the view is activated, false otherwise |
| 11542 | */ |
| 11543 | @ViewDebug.ExportedProperty |
| 11544 | public boolean isActivated() { |
| 11545 | return (mPrivateFlags & ACTIVATED) != 0; |
| 11546 | } |
| 11547 | |
| 11548 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11549 | * Returns the ViewTreeObserver for this view's hierarchy. The view tree |
| 11550 | * observer can be used to get notifications when global events, like |
| 11551 | * layout, happen. |
| 11552 | * |
| 11553 | * The returned ViewTreeObserver observer is not guaranteed to remain |
| 11554 | * valid for the lifetime of this View. If the caller of this method keeps |
| 11555 | * a long-lived reference to ViewTreeObserver, it should always check for |
| 11556 | * the return value of {@link ViewTreeObserver#isAlive()}. |
| 11557 | * |
| 11558 | * @return The ViewTreeObserver for this view's hierarchy. |
| 11559 | */ |
| 11560 | public ViewTreeObserver getViewTreeObserver() { |
| 11561 | if (mAttachInfo != null) { |
| 11562 | return mAttachInfo.mTreeObserver; |
| 11563 | } |
| 11564 | if (mFloatingTreeObserver == null) { |
| 11565 | mFloatingTreeObserver = new ViewTreeObserver(); |
| 11566 | } |
| 11567 | return mFloatingTreeObserver; |
| 11568 | } |
| 11569 | |
| 11570 | /** |
| 11571 | * <p>Finds the topmost view in the current view hierarchy.</p> |
| 11572 | * |
| 11573 | * @return the topmost view containing this view |
| 11574 | */ |
| 11575 | public View getRootView() { |
| 11576 | if (mAttachInfo != null) { |
| 11577 | final View v = mAttachInfo.mRootView; |
| 11578 | if (v != null) { |
| 11579 | return v; |
| 11580 | } |
| 11581 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11582 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11583 | View parent = this; |
| 11584 | |
| 11585 | while (parent.mParent != null && parent.mParent instanceof View) { |
| 11586 | parent = (View) parent.mParent; |
| 11587 | } |
| 11588 | |
| 11589 | return parent; |
| 11590 | } |
| 11591 | |
| 11592 | /** |
| 11593 | * <p>Computes the coordinates of this view on the screen. The argument |
| 11594 | * must be an array of two integers. After the method returns, the array |
| 11595 | * contains the x and y location in that order.</p> |
| 11596 | * |
| 11597 | * @param location an array of two integers in which to hold the coordinates |
| 11598 | */ |
| 11599 | public void getLocationOnScreen(int[] location) { |
| 11600 | getLocationInWindow(location); |
| 11601 | |
| 11602 | final AttachInfo info = mAttachInfo; |
Romain Guy | 779398e | 2009-06-16 13:17:50 -0700 | [diff] [blame] | 11603 | if (info != null) { |
| 11604 | location[0] += info.mWindowLeft; |
| 11605 | location[1] += info.mWindowTop; |
| 11606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11607 | } |
| 11608 | |
| 11609 | /** |
| 11610 | * <p>Computes the coordinates of this view in its window. The argument |
| 11611 | * must be an array of two integers. After the method returns, the array |
| 11612 | * contains the x and y location in that order.</p> |
| 11613 | * |
| 11614 | * @param location an array of two integers in which to hold the coordinates |
| 11615 | */ |
| 11616 | public void getLocationInWindow(int[] location) { |
| 11617 | if (location == null || location.length < 2) { |
| 11618 | throw new IllegalArgumentException("location must be an array of " |
| 11619 | + "two integers"); |
| 11620 | } |
| 11621 | |
Michael Jurka | 4d2bd4c | 2010-11-30 18:15:11 -0800 | [diff] [blame] | 11622 | location[0] = mLeft + (int) (mTranslationX + 0.5f); |
| 11623 | location[1] = mTop + (int) (mTranslationY + 0.5f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11624 | |
| 11625 | ViewParent viewParent = mParent; |
| 11626 | while (viewParent instanceof View) { |
| 11627 | final View view = (View)viewParent; |
Michael Jurka | 4d2bd4c | 2010-11-30 18:15:11 -0800 | [diff] [blame] | 11628 | location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX; |
| 11629 | location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11630 | viewParent = view.mParent; |
| 11631 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11632 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 11633 | if (viewParent instanceof ViewRootImpl) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11634 | // *cough* |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 11635 | final ViewRootImpl vr = (ViewRootImpl)viewParent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11636 | location[1] -= vr.mCurScrollY; |
| 11637 | } |
| 11638 | } |
| 11639 | |
| 11640 | /** |
| 11641 | * {@hide} |
| 11642 | * @param id the id of the view to be found |
| 11643 | * @return the view of the specified id, null if cannot be found |
| 11644 | */ |
| 11645 | protected View findViewTraversal(int id) { |
| 11646 | if (id == mID) { |
| 11647 | return this; |
| 11648 | } |
| 11649 | return null; |
| 11650 | } |
| 11651 | |
| 11652 | /** |
| 11653 | * {@hide} |
| 11654 | * @param tag the tag of the view to be found |
| 11655 | * @return the view of specified tag, null if cannot be found |
| 11656 | */ |
| 11657 | protected View findViewWithTagTraversal(Object tag) { |
| 11658 | if (tag != null && tag.equals(mTag)) { |
| 11659 | return this; |
| 11660 | } |
| 11661 | return null; |
| 11662 | } |
| 11663 | |
| 11664 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 11665 | * {@hide} |
| 11666 | * @param predicate The predicate to evaluate. |
| 11667 | * @return The first view that matches the predicate or null. |
| 11668 | */ |
| 11669 | protected View findViewByPredicateTraversal(Predicate<View> predicate) { |
| 11670 | if (predicate.apply(this)) { |
| 11671 | return this; |
| 11672 | } |
| 11673 | return null; |
| 11674 | } |
| 11675 | |
| 11676 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11677 | * Look for a child view with the given id. If this view has the given |
| 11678 | * id, return this view. |
| 11679 | * |
| 11680 | * @param id The id to search for. |
| 11681 | * @return The view that has the given id in the hierarchy or null |
| 11682 | */ |
| 11683 | public final View findViewById(int id) { |
| 11684 | if (id < 0) { |
| 11685 | return null; |
| 11686 | } |
| 11687 | return findViewTraversal(id); |
| 11688 | } |
| 11689 | |
| 11690 | /** |
| 11691 | * Look for a child view with the given tag. If this view has the given |
| 11692 | * tag, return this view. |
| 11693 | * |
| 11694 | * @param tag The tag to search for, using "tag.equals(getTag())". |
| 11695 | * @return The View that has the given tag in the hierarchy or null |
| 11696 | */ |
| 11697 | public final View findViewWithTag(Object tag) { |
| 11698 | if (tag == null) { |
| 11699 | return null; |
| 11700 | } |
| 11701 | return findViewWithTagTraversal(tag); |
| 11702 | } |
| 11703 | |
| 11704 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 11705 | * {@hide} |
| 11706 | * Look for a child view that matches the specified predicate. |
| 11707 | * If this view matches the predicate, return this view. |
| 11708 | * |
| 11709 | * @param predicate The predicate to evaluate. |
| 11710 | * @return The first view that matches the predicate or null. |
| 11711 | */ |
| 11712 | public final View findViewByPredicate(Predicate<View> predicate) { |
| 11713 | return findViewByPredicateTraversal(predicate); |
| 11714 | } |
| 11715 | |
| 11716 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11717 | * Sets the identifier for this view. The identifier does not have to be |
| 11718 | * unique in this view's hierarchy. The identifier should be a positive |
| 11719 | * number. |
| 11720 | * |
| 11721 | * @see #NO_ID |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11722 | * @see #getId() |
| 11723 | * @see #findViewById(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11724 | * |
| 11725 | * @param id a number used to identify the view |
| 11726 | * |
| 11727 | * @attr ref android.R.styleable#View_id |
| 11728 | */ |
| 11729 | public void setId(int id) { |
| 11730 | mID = id; |
| 11731 | } |
| 11732 | |
| 11733 | /** |
| 11734 | * {@hide} |
| 11735 | * |
| 11736 | * @param isRoot true if the view belongs to the root namespace, false |
| 11737 | * otherwise |
| 11738 | */ |
| 11739 | public void setIsRootNamespace(boolean isRoot) { |
| 11740 | if (isRoot) { |
| 11741 | mPrivateFlags |= IS_ROOT_NAMESPACE; |
| 11742 | } else { |
| 11743 | mPrivateFlags &= ~IS_ROOT_NAMESPACE; |
| 11744 | } |
| 11745 | } |
| 11746 | |
| 11747 | /** |
| 11748 | * {@hide} |
| 11749 | * |
| 11750 | * @return true if the view belongs to the root namespace, false otherwise |
| 11751 | */ |
| 11752 | public boolean isRootNamespace() { |
| 11753 | return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0; |
| 11754 | } |
| 11755 | |
| 11756 | /** |
| 11757 | * Returns this view's identifier. |
| 11758 | * |
| 11759 | * @return a positive integer used to identify the view or {@link #NO_ID} |
| 11760 | * if the view has no ID |
| 11761 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 11762 | * @see #setId(int) |
| 11763 | * @see #findViewById(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11764 | * @attr ref android.R.styleable#View_id |
| 11765 | */ |
| 11766 | @ViewDebug.CapturedViewProperty |
| 11767 | public int getId() { |
| 11768 | return mID; |
| 11769 | } |
| 11770 | |
| 11771 | /** |
| 11772 | * Returns this view's tag. |
| 11773 | * |
| 11774 | * @return the Object stored in this view as a tag |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11775 | * |
| 11776 | * @see #setTag(Object) |
| 11777 | * @see #getTag(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11778 | */ |
| 11779 | @ViewDebug.ExportedProperty |
| 11780 | public Object getTag() { |
| 11781 | return mTag; |
| 11782 | } |
| 11783 | |
| 11784 | /** |
| 11785 | * Sets the tag associated with this view. A tag can be used to mark |
| 11786 | * a view in its hierarchy and does not have to be unique within the |
| 11787 | * hierarchy. Tags can also be used to store data within a view without |
| 11788 | * resorting to another data structure. |
| 11789 | * |
| 11790 | * @param tag an Object to tag the view with |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11791 | * |
| 11792 | * @see #getTag() |
| 11793 | * @see #setTag(int, Object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11794 | */ |
| 11795 | public void setTag(final Object tag) { |
| 11796 | mTag = tag; |
| 11797 | } |
| 11798 | |
| 11799 | /** |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11800 | * Returns the tag associated with this view and the specified key. |
| 11801 | * |
| 11802 | * @param key The key identifying the tag |
| 11803 | * |
| 11804 | * @return the Object stored in this view as a tag |
| 11805 | * |
| 11806 | * @see #setTag(int, Object) |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11807 | * @see #getTag() |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11808 | */ |
| 11809 | public Object getTag(int key) { |
| 11810 | SparseArray<Object> tags = null; |
| 11811 | synchronized (sTagsLock) { |
| 11812 | if (sTags != null) { |
| 11813 | tags = sTags.get(this); |
| 11814 | } |
| 11815 | } |
| 11816 | |
| 11817 | if (tags != null) return tags.get(key); |
| 11818 | return null; |
| 11819 | } |
| 11820 | |
| 11821 | /** |
| 11822 | * Sets a tag associated with this view and a key. A tag can be used |
| 11823 | * to mark a view in its hierarchy and does not have to be unique within |
| 11824 | * the hierarchy. Tags can also be used to store data within a view |
| 11825 | * without resorting to another data structure. |
| 11826 | * |
| 11827 | * The specified key should be an id declared in the resources of the |
Scott Main | dfe5c20 | 2010-06-08 15:54:52 -0700 | [diff] [blame] | 11828 | * application to ensure it is unique (see the <a |
| 11829 | * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>). |
| 11830 | * Keys identified as belonging to |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11831 | * the Android framework or not associated with any package will cause |
| 11832 | * an {@link IllegalArgumentException} to be thrown. |
| 11833 | * |
| 11834 | * @param key The key identifying the tag |
| 11835 | * @param tag An Object to tag the view with |
| 11836 | * |
| 11837 | * @throws IllegalArgumentException If they specified key is not valid |
| 11838 | * |
| 11839 | * @see #setTag(Object) |
| 11840 | * @see #getTag(int) |
| 11841 | */ |
| 11842 | public void setTag(int key, final Object tag) { |
| 11843 | // If the package id is 0x00 or 0x01, it's either an undefined package |
| 11844 | // or a framework id |
| 11845 | if ((key >>> 24) < 2) { |
| 11846 | throw new IllegalArgumentException("The key must be an application-specific " |
| 11847 | + "resource id."); |
| 11848 | } |
| 11849 | |
| 11850 | setTagInternal(this, key, tag); |
| 11851 | } |
| 11852 | |
| 11853 | /** |
| 11854 | * Variation of {@link #setTag(int, Object)} that enforces the key to be a |
| 11855 | * framework id. |
| 11856 | * |
| 11857 | * @hide |
| 11858 | */ |
| 11859 | public void setTagInternal(int key, Object tag) { |
| 11860 | if ((key >>> 24) != 0x1) { |
| 11861 | throw new IllegalArgumentException("The key must be a framework-specific " |
| 11862 | + "resource id."); |
| 11863 | } |
| 11864 | |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11865 | setTagInternal(this, key, tag); |
Romain Guy | d90a331 | 2009-05-06 14:54:28 -0700 | [diff] [blame] | 11866 | } |
| 11867 | |
| 11868 | private static void setTagInternal(View view, int key, Object tag) { |
| 11869 | SparseArray<Object> tags = null; |
| 11870 | synchronized (sTagsLock) { |
| 11871 | if (sTags == null) { |
| 11872 | sTags = new WeakHashMap<View, SparseArray<Object>>(); |
| 11873 | } else { |
| 11874 | tags = sTags.get(view); |
| 11875 | } |
| 11876 | } |
| 11877 | |
| 11878 | if (tags == null) { |
| 11879 | tags = new SparseArray<Object>(2); |
| 11880 | synchronized (sTagsLock) { |
| 11881 | sTags.put(view, tags); |
| 11882 | } |
| 11883 | } |
| 11884 | |
| 11885 | tags.put(key, tag); |
| 11886 | } |
| 11887 | |
| 11888 | /** |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 11889 | * @param consistency The type of consistency. See ViewDebug for more information. |
| 11890 | * |
| 11891 | * @hide |
| 11892 | */ |
| 11893 | protected boolean dispatchConsistencyCheck(int consistency) { |
| 11894 | return onConsistencyCheck(consistency); |
| 11895 | } |
| 11896 | |
| 11897 | /** |
| 11898 | * Method that subclasses should implement to check their consistency. The type of |
| 11899 | * consistency check is indicated by the bit field passed as a parameter. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 11900 | * |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 11901 | * @param consistency The type of consistency. See ViewDebug for more information. |
| 11902 | * |
| 11903 | * @throws IllegalStateException if the view is in an inconsistent state. |
| 11904 | * |
| 11905 | * @hide |
| 11906 | */ |
| 11907 | protected boolean onConsistencyCheck(int consistency) { |
| 11908 | boolean result = true; |
| 11909 | |
| 11910 | final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0; |
| 11911 | final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0; |
| 11912 | |
| 11913 | if (checkLayout) { |
| 11914 | if (getParent() == null) { |
| 11915 | result = false; |
| 11916 | android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG, |
| 11917 | "View " + this + " does not have a parent."); |
| 11918 | } |
| 11919 | |
| 11920 | if (mAttachInfo == null) { |
| 11921 | result = false; |
| 11922 | android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG, |
| 11923 | "View " + this + " is not attached to a window."); |
| 11924 | } |
| 11925 | } |
| 11926 | |
| 11927 | if (checkDrawing) { |
| 11928 | // Do not check the DIRTY/DRAWN flags because views can call invalidate() |
| 11929 | // from their draw() method |
| 11930 | |
| 11931 | if ((mPrivateFlags & DRAWN) != DRAWN && |
| 11932 | (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) { |
| 11933 | result = false; |
| 11934 | android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG, |
| 11935 | "View " + this + " was invalidated but its drawing cache is valid."); |
| 11936 | } |
| 11937 | } |
| 11938 | |
| 11939 | return result; |
| 11940 | } |
| 11941 | |
| 11942 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 11943 | * Prints information about this view in the log output, with the tag |
| 11944 | * {@link #VIEW_LOG_TAG}. |
| 11945 | * |
| 11946 | * @hide |
| 11947 | */ |
| 11948 | public void debug() { |
| 11949 | debug(0); |
| 11950 | } |
| 11951 | |
| 11952 | /** |
| 11953 | * Prints information about this view in the log output, with the tag |
| 11954 | * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an |
| 11955 | * indentation defined by the <code>depth</code>. |
| 11956 | * |
| 11957 | * @param depth the indentation level |
| 11958 | * |
| 11959 | * @hide |
| 11960 | */ |
| 11961 | protected void debug(int depth) { |
| 11962 | String output = debugIndent(depth - 1); |
| 11963 | |
| 11964 | output += "+ " + this; |
| 11965 | int id = getId(); |
| 11966 | if (id != -1) { |
| 11967 | output += " (id=" + id + ")"; |
| 11968 | } |
| 11969 | Object tag = getTag(); |
| 11970 | if (tag != null) { |
| 11971 | output += " (tag=" + tag + ")"; |
| 11972 | } |
| 11973 | Log.d(VIEW_LOG_TAG, output); |
| 11974 | |
| 11975 | if ((mPrivateFlags & FOCUSED) != 0) { |
| 11976 | output = debugIndent(depth) + " FOCUSED"; |
| 11977 | Log.d(VIEW_LOG_TAG, output); |
| 11978 | } |
| 11979 | |
| 11980 | output = debugIndent(depth); |
| 11981 | output += "frame={" + mLeft + ", " + mTop + ", " + mRight |
| 11982 | + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY |
| 11983 | + "} "; |
| 11984 | Log.d(VIEW_LOG_TAG, output); |
| 11985 | |
| 11986 | if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0 |
| 11987 | || mPaddingBottom != 0) { |
| 11988 | output = debugIndent(depth); |
| 11989 | output += "padding={" + mPaddingLeft + ", " + mPaddingTop |
| 11990 | + ", " + mPaddingRight + ", " + mPaddingBottom + "}"; |
| 11991 | Log.d(VIEW_LOG_TAG, output); |
| 11992 | } |
| 11993 | |
| 11994 | output = debugIndent(depth); |
| 11995 | output += "mMeasureWidth=" + mMeasuredWidth + |
| 11996 | " mMeasureHeight=" + mMeasuredHeight; |
| 11997 | Log.d(VIEW_LOG_TAG, output); |
| 11998 | |
| 11999 | output = debugIndent(depth); |
| 12000 | if (mLayoutParams == null) { |
| 12001 | output += "BAD! no layout params"; |
| 12002 | } else { |
| 12003 | output = mLayoutParams.debug(output); |
| 12004 | } |
| 12005 | Log.d(VIEW_LOG_TAG, output); |
| 12006 | |
| 12007 | output = debugIndent(depth); |
| 12008 | output += "flags={"; |
| 12009 | output += View.printFlags(mViewFlags); |
| 12010 | output += "}"; |
| 12011 | Log.d(VIEW_LOG_TAG, output); |
| 12012 | |
| 12013 | output = debugIndent(depth); |
| 12014 | output += "privateFlags={"; |
| 12015 | output += View.printPrivateFlags(mPrivateFlags); |
| 12016 | output += "}"; |
| 12017 | Log.d(VIEW_LOG_TAG, output); |
| 12018 | } |
| 12019 | |
| 12020 | /** |
| 12021 | * Creates an string of whitespaces used for indentation. |
| 12022 | * |
| 12023 | * @param depth the indentation level |
| 12024 | * @return a String containing (depth * 2 + 3) * 2 white spaces |
| 12025 | * |
| 12026 | * @hide |
| 12027 | */ |
| 12028 | protected static String debugIndent(int depth) { |
| 12029 | StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2); |
| 12030 | for (int i = 0; i < (depth * 2) + 3; i++) { |
| 12031 | spaces.append(' ').append(' '); |
| 12032 | } |
| 12033 | return spaces.toString(); |
| 12034 | } |
| 12035 | |
| 12036 | /** |
| 12037 | * <p>Return the offset of the widget's text baseline from the widget's top |
| 12038 | * boundary. If this widget does not support baseline alignment, this |
| 12039 | * method returns -1. </p> |
| 12040 | * |
| 12041 | * @return the offset of the baseline within the widget's bounds or -1 |
| 12042 | * if baseline alignment is not supported |
| 12043 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 12044 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12045 | public int getBaseline() { |
| 12046 | return -1; |
| 12047 | } |
| 12048 | |
| 12049 | /** |
| 12050 | * Call this when something has changed which has invalidated the |
| 12051 | * layout of this view. This will schedule a layout pass of the view |
| 12052 | * tree. |
| 12053 | */ |
| 12054 | public void requestLayout() { |
| 12055 | if (ViewDebug.TRACE_HIERARCHY) { |
| 12056 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT); |
| 12057 | } |
| 12058 | |
| 12059 | mPrivateFlags |= FORCE_LAYOUT; |
Chet Haase | 5af048c | 2011-01-24 17:00:32 -0800 | [diff] [blame] | 12060 | mPrivateFlags |= INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12061 | |
Fabrice Di Meglio | d794aca | 2011-07-22 18:19:36 -0700 | [diff] [blame] | 12062 | if (mParent != null) { |
| 12063 | if (mLayoutParams != null) { |
| 12064 | mLayoutParams.resolveWithDirection(getResolvedLayoutDirection()); |
| 12065 | } |
| 12066 | if (!mParent.isLayoutRequested()) { |
| 12067 | mParent.requestLayout(); |
| 12068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12069 | } |
| 12070 | } |
| 12071 | |
| 12072 | /** |
| 12073 | * Forces this view to be laid out during the next layout pass. |
| 12074 | * This method does not call requestLayout() or forceLayout() |
| 12075 | * on the parent. |
| 12076 | */ |
| 12077 | public void forceLayout() { |
| 12078 | mPrivateFlags |= FORCE_LAYOUT; |
Chet Haase | 5af048c | 2011-01-24 17:00:32 -0800 | [diff] [blame] | 12079 | mPrivateFlags |= INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12080 | } |
| 12081 | |
| 12082 | /** |
| 12083 | * <p> |
| 12084 | * This is called to find out how big a view should be. The parent |
| 12085 | * supplies constraint information in the width and height parameters. |
| 12086 | * </p> |
| 12087 | * |
| 12088 | * <p> |
| 12089 | * The actual mesurement work of a view is performed in |
| 12090 | * {@link #onMeasure(int, int)}, called by this method. Therefore, only |
| 12091 | * {@link #onMeasure(int, int)} can and must be overriden by subclasses. |
| 12092 | * </p> |
| 12093 | * |
| 12094 | * |
| 12095 | * @param widthMeasureSpec Horizontal space requirements as imposed by the |
| 12096 | * parent |
| 12097 | * @param heightMeasureSpec Vertical space requirements as imposed by the |
| 12098 | * parent |
| 12099 | * |
| 12100 | * @see #onMeasure(int, int) |
| 12101 | */ |
| 12102 | public final void measure(int widthMeasureSpec, int heightMeasureSpec) { |
| 12103 | if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT || |
| 12104 | widthMeasureSpec != mOldWidthMeasureSpec || |
| 12105 | heightMeasureSpec != mOldHeightMeasureSpec) { |
| 12106 | |
| 12107 | // first clears the measured dimension flag |
| 12108 | mPrivateFlags &= ~MEASURED_DIMENSION_SET; |
| 12109 | |
| 12110 | if (ViewDebug.TRACE_HIERARCHY) { |
| 12111 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE); |
| 12112 | } |
| 12113 | |
| 12114 | // measure ourselves, this should set the measured dimension flag back |
| 12115 | onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 12116 | |
| 12117 | // flag not set, setMeasuredDimension() was not invoked, we raise |
| 12118 | // an exception to warn the developer |
| 12119 | if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) { |
| 12120 | throw new IllegalStateException("onMeasure() did not set the" |
| 12121 | + " measured dimension by calling" |
| 12122 | + " setMeasuredDimension()"); |
| 12123 | } |
| 12124 | |
| 12125 | mPrivateFlags |= LAYOUT_REQUIRED; |
| 12126 | } |
| 12127 | |
| 12128 | mOldWidthMeasureSpec = widthMeasureSpec; |
| 12129 | mOldHeightMeasureSpec = heightMeasureSpec; |
| 12130 | } |
| 12131 | |
| 12132 | /** |
| 12133 | * <p> |
| 12134 | * Measure the view and its content to determine the measured width and the |
| 12135 | * measured height. This method is invoked by {@link #measure(int, int)} and |
| 12136 | * should be overriden by subclasses to provide accurate and efficient |
| 12137 | * measurement of their contents. |
| 12138 | * </p> |
| 12139 | * |
| 12140 | * <p> |
| 12141 | * <strong>CONTRACT:</strong> When overriding this method, you |
| 12142 | * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the |
| 12143 | * measured width and height of this view. Failure to do so will trigger an |
| 12144 | * <code>IllegalStateException</code>, thrown by |
| 12145 | * {@link #measure(int, int)}. Calling the superclass' |
| 12146 | * {@link #onMeasure(int, int)} is a valid use. |
| 12147 | * </p> |
| 12148 | * |
| 12149 | * <p> |
| 12150 | * The base class implementation of measure defaults to the background size, |
| 12151 | * unless a larger size is allowed by the MeasureSpec. Subclasses should |
| 12152 | * override {@link #onMeasure(int, int)} to provide better measurements of |
| 12153 | * their content. |
| 12154 | * </p> |
| 12155 | * |
| 12156 | * <p> |
| 12157 | * If this method is overridden, it is the subclass's responsibility to make |
| 12158 | * sure the measured height and width are at least the view's minimum height |
| 12159 | * and width ({@link #getSuggestedMinimumHeight()} and |
| 12160 | * {@link #getSuggestedMinimumWidth()}). |
| 12161 | * </p> |
| 12162 | * |
| 12163 | * @param widthMeasureSpec horizontal space requirements as imposed by the parent. |
| 12164 | * The requirements are encoded with |
| 12165 | * {@link android.view.View.MeasureSpec}. |
| 12166 | * @param heightMeasureSpec vertical space requirements as imposed by the parent. |
| 12167 | * The requirements are encoded with |
| 12168 | * {@link android.view.View.MeasureSpec}. |
| 12169 | * |
| 12170 | * @see #getMeasuredWidth() |
| 12171 | * @see #getMeasuredHeight() |
| 12172 | * @see #setMeasuredDimension(int, int) |
| 12173 | * @see #getSuggestedMinimumHeight() |
| 12174 | * @see #getSuggestedMinimumWidth() |
| 12175 | * @see android.view.View.MeasureSpec#getMode(int) |
| 12176 | * @see android.view.View.MeasureSpec#getSize(int) |
| 12177 | */ |
| 12178 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 12179 | setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), |
| 12180 | getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); |
| 12181 | } |
| 12182 | |
| 12183 | /** |
| 12184 | * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the |
| 12185 | * measured width and measured height. Failing to do so will trigger an |
| 12186 | * exception at measurement time.</p> |
| 12187 | * |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12188 | * @param measuredWidth The measured width of this view. May be a complex |
| 12189 | * bit mask as defined by {@link #MEASURED_SIZE_MASK} and |
| 12190 | * {@link #MEASURED_STATE_TOO_SMALL}. |
| 12191 | * @param measuredHeight The measured height of this view. May be a complex |
| 12192 | * bit mask as defined by {@link #MEASURED_SIZE_MASK} and |
| 12193 | * {@link #MEASURED_STATE_TOO_SMALL}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12194 | */ |
| 12195 | protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) { |
| 12196 | mMeasuredWidth = measuredWidth; |
| 12197 | mMeasuredHeight = measuredHeight; |
| 12198 | |
| 12199 | mPrivateFlags |= MEASURED_DIMENSION_SET; |
| 12200 | } |
| 12201 | |
| 12202 | /** |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12203 | * Merge two states as returned by {@link #getMeasuredState()}. |
| 12204 | * @param curState The current state as returned from a view or the result |
| 12205 | * of combining multiple views. |
| 12206 | * @param newState The new view state to combine. |
| 12207 | * @return Returns a new integer reflecting the combination of the two |
| 12208 | * states. |
| 12209 | */ |
| 12210 | public static int combineMeasuredStates(int curState, int newState) { |
| 12211 | return curState | newState; |
| 12212 | } |
| 12213 | |
| 12214 | /** |
| 12215 | * Version of {@link #resolveSizeAndState(int, int, int)} |
| 12216 | * returning only the {@link #MEASURED_SIZE_MASK} bits of the result. |
| 12217 | */ |
| 12218 | public static int resolveSize(int size, int measureSpec) { |
| 12219 | return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK; |
| 12220 | } |
| 12221 | |
| 12222 | /** |
| 12223 | * Utility to reconcile a desired size and state, with constraints imposed |
| 12224 | * by a MeasureSpec. Will take the desired size, unless a different size |
| 12225 | * is imposed by the constraints. The returned value is a compound integer, |
| 12226 | * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and |
| 12227 | * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting |
| 12228 | * size is smaller than the size the view wants to be. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12229 | * |
| 12230 | * @param size How big the view wants to be |
| 12231 | * @param measureSpec Constraints imposed by the parent |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12232 | * @return Size information bit mask as defined by |
| 12233 | * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12234 | */ |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12235 | public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12236 | int result = size; |
| 12237 | int specMode = MeasureSpec.getMode(measureSpec); |
| 12238 | int specSize = MeasureSpec.getSize(measureSpec); |
| 12239 | switch (specMode) { |
| 12240 | case MeasureSpec.UNSPECIFIED: |
| 12241 | result = size; |
| 12242 | break; |
| 12243 | case MeasureSpec.AT_MOST: |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12244 | if (specSize < size) { |
| 12245 | result = specSize | MEASURED_STATE_TOO_SMALL; |
| 12246 | } else { |
| 12247 | result = size; |
| 12248 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12249 | break; |
| 12250 | case MeasureSpec.EXACTLY: |
| 12251 | result = specSize; |
| 12252 | break; |
| 12253 | } |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 12254 | return result | (childMeasuredState&MEASURED_STATE_MASK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12255 | } |
| 12256 | |
| 12257 | /** |
| 12258 | * Utility to return a default size. Uses the supplied size if the |
Romain Guy | 98029c8 | 2011-06-17 15:47:07 -0700 | [diff] [blame] | 12259 | * MeasureSpec imposed no constraints. Will get larger if allowed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12260 | * by the MeasureSpec. |
| 12261 | * |
| 12262 | * @param size Default size for this view |
| 12263 | * @param measureSpec Constraints imposed by the parent |
| 12264 | * @return The size this view should be. |
| 12265 | */ |
| 12266 | public static int getDefaultSize(int size, int measureSpec) { |
| 12267 | int result = size; |
| 12268 | int specMode = MeasureSpec.getMode(measureSpec); |
Romain Guy | 98029c8 | 2011-06-17 15:47:07 -0700 | [diff] [blame] | 12269 | int specSize = MeasureSpec.getSize(measureSpec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12270 | |
| 12271 | switch (specMode) { |
| 12272 | case MeasureSpec.UNSPECIFIED: |
| 12273 | result = size; |
| 12274 | break; |
| 12275 | case MeasureSpec.AT_MOST: |
| 12276 | case MeasureSpec.EXACTLY: |
| 12277 | result = specSize; |
| 12278 | break; |
| 12279 | } |
| 12280 | return result; |
| 12281 | } |
| 12282 | |
| 12283 | /** |
| 12284 | * Returns the suggested minimum height that the view should use. This |
| 12285 | * returns the maximum of the view's minimum height |
| 12286 | * and the background's minimum height |
| 12287 | * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}). |
| 12288 | * <p> |
| 12289 | * When being used in {@link #onMeasure(int, int)}, the caller should still |
| 12290 | * ensure the returned height is within the requirements of the parent. |
| 12291 | * |
| 12292 | * @return The suggested minimum height of the view. |
| 12293 | */ |
| 12294 | protected int getSuggestedMinimumHeight() { |
| 12295 | int suggestedMinHeight = mMinHeight; |
| 12296 | |
| 12297 | if (mBGDrawable != null) { |
| 12298 | final int bgMinHeight = mBGDrawable.getMinimumHeight(); |
| 12299 | if (suggestedMinHeight < bgMinHeight) { |
| 12300 | suggestedMinHeight = bgMinHeight; |
| 12301 | } |
| 12302 | } |
| 12303 | |
| 12304 | return suggestedMinHeight; |
| 12305 | } |
| 12306 | |
| 12307 | /** |
| 12308 | * Returns the suggested minimum width that the view should use. This |
| 12309 | * returns the maximum of the view's minimum width) |
| 12310 | * and the background's minimum width |
| 12311 | * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}). |
| 12312 | * <p> |
| 12313 | * When being used in {@link #onMeasure(int, int)}, the caller should still |
| 12314 | * ensure the returned width is within the requirements of the parent. |
| 12315 | * |
| 12316 | * @return The suggested minimum width of the view. |
| 12317 | */ |
| 12318 | protected int getSuggestedMinimumWidth() { |
| 12319 | int suggestedMinWidth = mMinWidth; |
| 12320 | |
| 12321 | if (mBGDrawable != null) { |
| 12322 | final int bgMinWidth = mBGDrawable.getMinimumWidth(); |
| 12323 | if (suggestedMinWidth < bgMinWidth) { |
| 12324 | suggestedMinWidth = bgMinWidth; |
| 12325 | } |
| 12326 | } |
| 12327 | |
| 12328 | return suggestedMinWidth; |
| 12329 | } |
| 12330 | |
| 12331 | /** |
| 12332 | * Sets the minimum height of the view. It is not guaranteed the view will |
| 12333 | * be able to achieve this minimum height (for example, if its parent layout |
| 12334 | * constrains it with less available height). |
| 12335 | * |
| 12336 | * @param minHeight The minimum height the view will try to be. |
| 12337 | */ |
| 12338 | public void setMinimumHeight(int minHeight) { |
| 12339 | mMinHeight = minHeight; |
| 12340 | } |
| 12341 | |
| 12342 | /** |
| 12343 | * Sets the minimum width of the view. It is not guaranteed the view will |
| 12344 | * be able to achieve this minimum width (for example, if its parent layout |
| 12345 | * constrains it with less available width). |
| 12346 | * |
| 12347 | * @param minWidth The minimum width the view will try to be. |
| 12348 | */ |
| 12349 | public void setMinimumWidth(int minWidth) { |
| 12350 | mMinWidth = minWidth; |
| 12351 | } |
| 12352 | |
| 12353 | /** |
| 12354 | * Get the animation currently associated with this view. |
| 12355 | * |
| 12356 | * @return The animation that is currently playing or |
| 12357 | * scheduled to play for this view. |
| 12358 | */ |
| 12359 | public Animation getAnimation() { |
| 12360 | return mCurrentAnimation; |
| 12361 | } |
| 12362 | |
| 12363 | /** |
| 12364 | * Start the specified animation now. |
| 12365 | * |
| 12366 | * @param animation the animation to start now |
| 12367 | */ |
| 12368 | public void startAnimation(Animation animation) { |
| 12369 | animation.setStartTime(Animation.START_ON_FIRST_FRAME); |
| 12370 | setAnimation(animation); |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 12371 | invalidateParentCaches(); |
| 12372 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12373 | } |
| 12374 | |
| 12375 | /** |
| 12376 | * Cancels any animations for this view. |
| 12377 | */ |
| 12378 | public void clearAnimation() { |
Romain Guy | 305a2eb | 2010-02-09 11:30:44 -0800 | [diff] [blame] | 12379 | if (mCurrentAnimation != null) { |
Romain Guy | b4a107d | 2010-02-09 18:50:08 -0800 | [diff] [blame] | 12380 | mCurrentAnimation.detach(); |
Romain Guy | 305a2eb | 2010-02-09 11:30:44 -0800 | [diff] [blame] | 12381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12382 | mCurrentAnimation = null; |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 12383 | invalidateParentIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12384 | } |
| 12385 | |
| 12386 | /** |
| 12387 | * Sets the next animation to play for this view. |
| 12388 | * If you want the animation to play immediately, use |
| 12389 | * startAnimation. This method provides allows fine-grained |
| 12390 | * control over the start time and invalidation, but you |
| 12391 | * must make sure that 1) the animation has a start time set, and |
| 12392 | * 2) the view will be invalidated when the animation is supposed to |
| 12393 | * start. |
| 12394 | * |
| 12395 | * @param animation The next animation, or null. |
| 12396 | */ |
| 12397 | public void setAnimation(Animation animation) { |
| 12398 | mCurrentAnimation = animation; |
| 12399 | if (animation != null) { |
| 12400 | animation.reset(); |
| 12401 | } |
| 12402 | } |
| 12403 | |
| 12404 | /** |
| 12405 | * Invoked by a parent ViewGroup to notify the start of the animation |
| 12406 | * currently associated with this view. If you override this method, |
| 12407 | * always call super.onAnimationStart(); |
| 12408 | * |
| 12409 | * @see #setAnimation(android.view.animation.Animation) |
| 12410 | * @see #getAnimation() |
| 12411 | */ |
| 12412 | protected void onAnimationStart() { |
| 12413 | mPrivateFlags |= ANIMATION_STARTED; |
| 12414 | } |
| 12415 | |
| 12416 | /** |
| 12417 | * Invoked by a parent ViewGroup to notify the end of the animation |
| 12418 | * currently associated with this view. If you override this method, |
| 12419 | * always call super.onAnimationEnd(); |
| 12420 | * |
| 12421 | * @see #setAnimation(android.view.animation.Animation) |
| 12422 | * @see #getAnimation() |
| 12423 | */ |
| 12424 | protected void onAnimationEnd() { |
| 12425 | mPrivateFlags &= ~ANIMATION_STARTED; |
| 12426 | } |
| 12427 | |
| 12428 | /** |
| 12429 | * Invoked if there is a Transform that involves alpha. Subclass that can |
| 12430 | * draw themselves with the specified alpha should return true, and then |
| 12431 | * respect that alpha when their onDraw() is called. If this returns false |
| 12432 | * then the view may be redirected to draw into an offscreen buffer to |
| 12433 | * fulfill the request, which will look fine, but may be slower than if the |
| 12434 | * subclass handles it internally. The default implementation returns false. |
| 12435 | * |
| 12436 | * @param alpha The alpha (0..255) to apply to the view's drawing |
| 12437 | * @return true if the view can draw with the specified alpha. |
| 12438 | */ |
| 12439 | protected boolean onSetAlpha(int alpha) { |
| 12440 | return false; |
| 12441 | } |
| 12442 | |
| 12443 | /** |
| 12444 | * This is used by the RootView to perform an optimization when |
| 12445 | * the view hierarchy contains one or several SurfaceView. |
| 12446 | * SurfaceView is always considered transparent, but its children are not, |
| 12447 | * therefore all View objects remove themselves from the global transparent |
| 12448 | * region (passed as a parameter to this function). |
| 12449 | * |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 12450 | * @param region The transparent region for this ViewAncestor (window). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12451 | * |
| 12452 | * @return Returns true if the effective visibility of the view at this |
| 12453 | * point is opaque, regardless of the transparent region; returns false |
| 12454 | * if it is possible for underlying windows to be seen behind the view. |
| 12455 | * |
| 12456 | * {@hide} |
| 12457 | */ |
| 12458 | public boolean gatherTransparentRegion(Region region) { |
| 12459 | final AttachInfo attachInfo = mAttachInfo; |
| 12460 | if (region != null && attachInfo != null) { |
| 12461 | final int pflags = mPrivateFlags; |
| 12462 | if ((pflags & SKIP_DRAW) == 0) { |
| 12463 | // The SKIP_DRAW flag IS NOT set, so this view draws. We need to |
| 12464 | // remove it from the transparent region. |
| 12465 | final int[] location = attachInfo.mTransparentLocation; |
| 12466 | getLocationInWindow(location); |
| 12467 | region.op(location[0], location[1], location[0] + mRight - mLeft, |
| 12468 | location[1] + mBottom - mTop, Region.Op.DIFFERENCE); |
| 12469 | } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) { |
| 12470 | // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable |
| 12471 | // exists, so we remove the background drawable's non-transparent |
| 12472 | // parts from this transparent region. |
| 12473 | applyDrawableToTransparentRegion(mBGDrawable, region); |
| 12474 | } |
| 12475 | } |
| 12476 | return true; |
| 12477 | } |
| 12478 | |
| 12479 | /** |
| 12480 | * Play a sound effect for this view. |
| 12481 | * |
| 12482 | * <p>The framework will play sound effects for some built in actions, such as |
| 12483 | * clicking, but you may wish to play these effects in your widget, |
| 12484 | * for instance, for internal navigation. |
| 12485 | * |
| 12486 | * <p>The sound effect will only be played if sound effects are enabled by the user, and |
| 12487 | * {@link #isSoundEffectsEnabled()} is true. |
| 12488 | * |
| 12489 | * @param soundConstant One of the constants defined in {@link SoundEffectConstants} |
| 12490 | */ |
| 12491 | public void playSoundEffect(int soundConstant) { |
| 12492 | if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) { |
| 12493 | return; |
| 12494 | } |
| 12495 | mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant); |
| 12496 | } |
| 12497 | |
| 12498 | /** |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 12499 | * BZZZTT!!1! |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 12500 | * |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 12501 | * <p>Provide haptic feedback to the user for this view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12502 | * |
| 12503 | * <p>The framework will provide haptic feedback for some built in actions, |
| 12504 | * such as long presses, but you may wish to provide feedback for your |
| 12505 | * own widget. |
| 12506 | * |
| 12507 | * <p>The feedback will only be performed if |
| 12508 | * {@link #isHapticFeedbackEnabled()} is true. |
| 12509 | * |
| 12510 | * @param feedbackConstant One of the constants defined in |
| 12511 | * {@link HapticFeedbackConstants} |
| 12512 | */ |
| 12513 | public boolean performHapticFeedback(int feedbackConstant) { |
| 12514 | return performHapticFeedback(feedbackConstant, 0); |
| 12515 | } |
| 12516 | |
| 12517 | /** |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 12518 | * BZZZTT!!1! |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 12519 | * |
Andy Stadler | f8a7cea | 2009-04-10 16:24:47 -0700 | [diff] [blame] | 12520 | * <p>Like {@link #performHapticFeedback(int)}, with additional options. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12521 | * |
| 12522 | * @param feedbackConstant One of the constants defined in |
| 12523 | * {@link HapticFeedbackConstants} |
| 12524 | * @param flags Additional flags as per {@link HapticFeedbackConstants}. |
| 12525 | */ |
| 12526 | public boolean performHapticFeedback(int feedbackConstant, int flags) { |
| 12527 | if (mAttachInfo == null) { |
| 12528 | return false; |
| 12529 | } |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 12530 | //noinspection SimplifiableIfStatement |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 12531 | if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12532 | && !isHapticFeedbackEnabled()) { |
| 12533 | return false; |
| 12534 | } |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 12535 | return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant, |
| 12536 | (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12537 | } |
| 12538 | |
| 12539 | /** |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12540 | * Request that the visibility of the status bar be changed. |
Scott Main | ec6331b | 2011-05-24 16:55:56 -0700 | [diff] [blame] | 12541 | * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}. |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12542 | */ |
| 12543 | public void setSystemUiVisibility(int visibility) { |
| 12544 | if (visibility != mSystemUiVisibility) { |
| 12545 | mSystemUiVisibility = visibility; |
| 12546 | if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 12547 | mParent.recomputeViewAttributes(this); |
| 12548 | } |
| 12549 | } |
| 12550 | } |
| 12551 | |
| 12552 | /** |
| 12553 | * Returns the status bar visibility that this view has requested. |
Scott Main | ec6331b | 2011-05-24 16:55:56 -0700 | [diff] [blame] | 12554 | * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}. |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12555 | */ |
Joe Onorato | e595cad | 2011-01-24 09:22:12 -0800 | [diff] [blame] | 12556 | public int getSystemUiVisibility() { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12557 | return mSystemUiVisibility; |
| 12558 | } |
| 12559 | |
Scott Main | ec6331b | 2011-05-24 16:55:56 -0700 | [diff] [blame] | 12560 | /** |
| 12561 | * Set a listener to receive callbacks when the visibility of the system bar changes. |
| 12562 | * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks. |
| 12563 | */ |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12564 | public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) { |
| 12565 | mOnSystemUiVisibilityChangeListener = l; |
| 12566 | if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 12567 | mParent.recomputeViewAttributes(this); |
| 12568 | } |
| 12569 | } |
| 12570 | |
| 12571 | /** |
| 12572 | */ |
| 12573 | public void dispatchSystemUiVisibilityChanged(int visibility) { |
| 12574 | if (mOnSystemUiVisibilityChangeListener != null) { |
Joe Onorato | 7bb8eeb | 2011-01-27 16:00:58 -0800 | [diff] [blame] | 12575 | mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange( |
Joe Onorato | 6ab77bd | 2011-01-31 11:21:10 -0800 | [diff] [blame] | 12576 | visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK); |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 12577 | } |
| 12578 | } |
| 12579 | |
| 12580 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12581 | * Creates an image that the system displays during the drag and drop |
| 12582 | * operation. This is called a "drag shadow". The default implementation |
| 12583 | * for a DragShadowBuilder based on a View returns an image that has exactly the same |
| 12584 | * appearance as the given View. The default also positions the center of the drag shadow |
| 12585 | * directly under the touch point. If no View is provided (the constructor with no parameters |
| 12586 | * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and |
| 12587 | * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the |
| 12588 | * default is an invisible drag shadow. |
| 12589 | * <p> |
| 12590 | * You are not required to use the View you provide to the constructor as the basis of the |
| 12591 | * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw |
| 12592 | * anything you want as the drag shadow. |
| 12593 | * </p> |
| 12594 | * <p> |
| 12595 | * You pass a DragShadowBuilder object to the system when you start the drag. The system |
| 12596 | * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the |
| 12597 | * size and position of the drag shadow. It uses this data to construct a |
| 12598 | * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()} |
| 12599 | * so that your application can draw the shadow image in the Canvas. |
| 12600 | * </p> |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12601 | */ |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12602 | public static class DragShadowBuilder { |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12603 | private final WeakReference<View> mView; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12604 | |
| 12605 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12606 | * Constructs a shadow image builder based on a View. By default, the resulting drag |
| 12607 | * shadow will have the same appearance and dimensions as the View, with the touch point |
| 12608 | * over the center of the View. |
| 12609 | * @param view A View. Any View in scope can be used. |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12610 | */ |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12611 | public DragShadowBuilder(View view) { |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12612 | mView = new WeakReference<View>(view); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12613 | } |
| 12614 | |
Christopher Tate | 17ed60c | 2011-01-18 12:50:26 -0800 | [diff] [blame] | 12615 | /** |
| 12616 | * Construct a shadow builder object with no associated View. This |
| 12617 | * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)} |
| 12618 | * and {@link #onDrawShadow(Canvas)} methods are also overridden in order |
| 12619 | * to supply the drag shadow's dimensions and appearance without |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12620 | * reference to any View object. If they are not overridden, then the result is an |
| 12621 | * invisible drag shadow. |
Christopher Tate | 17ed60c | 2011-01-18 12:50:26 -0800 | [diff] [blame] | 12622 | */ |
| 12623 | public DragShadowBuilder() { |
| 12624 | mView = new WeakReference<View>(null); |
| 12625 | } |
| 12626 | |
| 12627 | /** |
| 12628 | * Returns the View object that had been passed to the |
| 12629 | * {@link #View.DragShadowBuilder(View)} |
| 12630 | * constructor. If that View parameter was {@code null} or if the |
| 12631 | * {@link #View.DragShadowBuilder()} |
| 12632 | * constructor was used to instantiate the builder object, this method will return |
| 12633 | * null. |
| 12634 | * |
| 12635 | * @return The View object associate with this builder object. |
| 12636 | */ |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 12637 | @SuppressWarnings({"JavadocReference"}) |
Chris Tate | 6b39128 | 2010-10-14 15:48:59 -0700 | [diff] [blame] | 12638 | final public View getView() { |
| 12639 | return mView.get(); |
| 12640 | } |
| 12641 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12642 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12643 | * Provides the metrics for the shadow image. These include the dimensions of |
| 12644 | * the shadow image, and the point within that shadow that should |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12645 | * be centered under the touch location while dragging. |
| 12646 | * <p> |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12647 | * The default implementation sets the dimensions of the shadow to be the |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12648 | * same as the dimensions of the View itself and centers the shadow under |
| 12649 | * the touch point. |
| 12650 | * </p> |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12651 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12652 | * @param shadowSize A {@link android.graphics.Point} containing the width and height |
| 12653 | * of the shadow image. Your application must set {@link android.graphics.Point#x} to the |
| 12654 | * desired width and must set {@link android.graphics.Point#y} to the desired height of the |
| 12655 | * image. |
| 12656 | * |
| 12657 | * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the |
| 12658 | * shadow image that should be underneath the touch point during the drag and drop |
| 12659 | * operation. Your application must set {@link android.graphics.Point#x} to the |
| 12660 | * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position. |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12661 | */ |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12662 | public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12663 | final View view = mView.get(); |
| 12664 | if (view != null) { |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12665 | shadowSize.set(view.getWidth(), view.getHeight()); |
| 12666 | shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2); |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12667 | } else { |
| 12668 | Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view"); |
| 12669 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12670 | } |
| 12671 | |
| 12672 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12673 | * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object |
| 12674 | * based on the dimensions it received from the |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12675 | * {@link #onProvideShadowMetrics(Point, Point)} callback. |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12676 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12677 | * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image. |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12678 | */ |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12679 | public void onDrawShadow(Canvas canvas) { |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12680 | final View view = mView.get(); |
| 12681 | if (view != null) { |
| 12682 | view.draw(canvas); |
| 12683 | } else { |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12684 | Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view"); |
Christopher Tate | a037419 | 2010-10-05 13:06:41 -0700 | [diff] [blame] | 12685 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12686 | } |
| 12687 | } |
| 12688 | |
| 12689 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12690 | * Starts a drag and drop operation. When your application calls this method, it passes a |
| 12691 | * {@link android.view.View.DragShadowBuilder} object to the system. The |
| 12692 | * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)} |
| 12693 | * to get metrics for the drag shadow, and then calls the object's |
| 12694 | * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself. |
| 12695 | * <p> |
| 12696 | * Once the system has the drag shadow, it begins the drag and drop operation by sending |
| 12697 | * drag events to all the View objects in your application that are currently visible. It does |
| 12698 | * this either by calling the View object's drag listener (an implementation of |
| 12699 | * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the |
| 12700 | * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method. |
| 12701 | * Both are passed a {@link android.view.DragEvent} object that has a |
| 12702 | * {@link android.view.DragEvent#getAction()} value of |
| 12703 | * {@link android.view.DragEvent#ACTION_DRAG_STARTED}. |
| 12704 | * </p> |
| 12705 | * <p> |
| 12706 | * Your application can invoke startDrag() on any attached View object. The View object does not |
| 12707 | * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to |
| 12708 | * be related to the View the user selected for dragging. |
| 12709 | * </p> |
| 12710 | * @param data A {@link android.content.ClipData} object pointing to the data to be |
| 12711 | * transferred by the drag and drop operation. |
| 12712 | * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the |
| 12713 | * drag shadow. |
| 12714 | * @param myLocalState An {@link java.lang.Object} containing local data about the drag and |
| 12715 | * drop operation. This Object is put into every DragEvent object sent by the system during the |
| 12716 | * current drag. |
| 12717 | * <p> |
| 12718 | * myLocalState is a lightweight mechanism for the sending information from the dragged View |
| 12719 | * to the target Views. For example, it can contain flags that differentiate between a |
| 12720 | * a copy operation and a move operation. |
| 12721 | * </p> |
| 12722 | * @param flags Flags that control the drag and drop operation. No flags are currently defined, |
| 12723 | * so the parameter should be set to 0. |
| 12724 | * @return {@code true} if the method completes successfully, or |
| 12725 | * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to |
| 12726 | * do a drag, and so no drag operation is in progress. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12727 | */ |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12728 | public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder, |
Christopher Tate | 02d2b3b | 2011-01-10 20:43:53 -0800 | [diff] [blame] | 12729 | Object myLocalState, int flags) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12730 | if (ViewDebug.DEBUG_DRAG) { |
Christopher Tate | 02d2b3b | 2011-01-10 20:43:53 -0800 | [diff] [blame] | 12731 | Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12732 | } |
| 12733 | boolean okay = false; |
| 12734 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12735 | Point shadowSize = new Point(); |
| 12736 | Point shadowTouchPoint = new Point(); |
| 12737 | shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12738 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12739 | if ((shadowSize.x < 0) || (shadowSize.y < 0) || |
| 12740 | (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) { |
| 12741 | throw new IllegalStateException("Drag shadow dimensions must not be negative"); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12742 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12743 | |
Chris Tate | a32dcf7 | 2010-10-14 12:13:50 -0700 | [diff] [blame] | 12744 | if (ViewDebug.DEBUG_DRAG) { |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12745 | Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y |
| 12746 | + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y); |
Chris Tate | a32dcf7 | 2010-10-14 12:13:50 -0700 | [diff] [blame] | 12747 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12748 | Surface surface = new Surface(); |
| 12749 | try { |
| 12750 | IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow, |
Christopher Tate | 02d2b3b | 2011-01-10 20:43:53 -0800 | [diff] [blame] | 12751 | flags, shadowSize.x, shadowSize.y, surface); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12752 | if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12753 | + " surface=" + surface); |
| 12754 | if (token != null) { |
| 12755 | Canvas canvas = surface.lockCanvas(null); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 12756 | try { |
Chris Tate | 6b39128 | 2010-10-14 15:48:59 -0700 | [diff] [blame] | 12757 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12758 | shadowBuilder.onDrawShadow(canvas); |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 12759 | } finally { |
| 12760 | surface.unlockCanvasAndPost(canvas); |
| 12761 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12762 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 12763 | final ViewRootImpl root = getViewRootImpl(); |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 12764 | |
| 12765 | // Cache the local state object for delivery with DragEvents |
| 12766 | root.setLocalDragState(myLocalState); |
| 12767 | |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12768 | // repurpose 'shadowSize' for the last touch point |
| 12769 | root.getLastTouchPoint(shadowSize); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12770 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12771 | okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token, |
Christopher Tate | 36d4c3f | 2011-01-07 13:34:24 -0800 | [diff] [blame] | 12772 | shadowSize.x, shadowSize.y, |
| 12773 | shadowTouchPoint.x, shadowTouchPoint.y, data); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 12774 | if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12775 | } |
| 12776 | } catch (Exception e) { |
| 12777 | Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e); |
| 12778 | surface.destroy(); |
| 12779 | } |
| 12780 | |
| 12781 | return okay; |
| 12782 | } |
| 12783 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12784 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12785 | * Handles drag events sent by the system following a call to |
| 12786 | * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}. |
| 12787 | *<p> |
| 12788 | * When the system calls this method, it passes a |
| 12789 | * {@link android.view.DragEvent} object. A call to |
| 12790 | * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined |
| 12791 | * in DragEvent. The method uses these to determine what is happening in the drag and drop |
| 12792 | * operation. |
| 12793 | * @param event The {@link android.view.DragEvent} sent by the system. |
| 12794 | * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined |
| 12795 | * in DragEvent, indicating the type of drag event represented by this object. |
| 12796 | * @return {@code true} if the method was successful, otherwise {@code false}. |
| 12797 | * <p> |
| 12798 | * The method should return {@code true} in response to an action type of |
| 12799 | * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current |
| 12800 | * operation. |
| 12801 | * </p> |
| 12802 | * <p> |
| 12803 | * The method should also return {@code true} in response to an action type of |
| 12804 | * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or |
| 12805 | * {@code false} if it didn't. |
| 12806 | * </p> |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12807 | */ |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 12808 | public boolean onDragEvent(DragEvent event) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12809 | return false; |
| 12810 | } |
| 12811 | |
| 12812 | /** |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12813 | * Detects if this View is enabled and has a drag event listener. |
| 12814 | * If both are true, then it calls the drag event listener with the |
| 12815 | * {@link android.view.DragEvent} it received. If the drag event listener returns |
| 12816 | * {@code true}, then dispatchDragEvent() returns {@code true}. |
| 12817 | * <p> |
| 12818 | * For all other cases, the method calls the |
| 12819 | * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler |
| 12820 | * method and returns its result. |
| 12821 | * </p> |
| 12822 | * <p> |
| 12823 | * This ensures that a drag event is always consumed, even if the View does not have a drag |
| 12824 | * event listener. However, if the View has a listener and the listener returns true, then |
| 12825 | * onDragEvent() is not called. |
| 12826 | * </p> |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12827 | */ |
| 12828 | public boolean dispatchDragEvent(DragEvent event) { |
Romain Guy | 676b173 | 2011-02-14 14:45:33 -0800 | [diff] [blame] | 12829 | //noinspection SimplifiableIfStatement |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 12830 | if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED |
| 12831 | && mOnDragListener.onDrag(this, event)) { |
| 12832 | return true; |
| 12833 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12834 | return onDragEvent(event); |
| 12835 | } |
| 12836 | |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 12837 | boolean canAcceptDrag() { |
| 12838 | return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0; |
| 12839 | } |
| 12840 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 12841 | /** |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 12842 | * This needs to be a better API (NOT ON VIEW) before it is exposed. If |
| 12843 | * it is ever exposed at all. |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 12844 | * @hide |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 12845 | */ |
| 12846 | public void onCloseSystemDialogs(String reason) { |
| 12847 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 12848 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 12849 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12850 | * Given a Drawable whose bounds have been set to draw into this view, |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 12851 | * update a Region being computed for |
| 12852 | * {@link #gatherTransparentRegion(android.graphics.Region)} so |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12853 | * that any non-transparent parts of the Drawable are removed from the |
| 12854 | * given transparent region. |
| 12855 | * |
| 12856 | * @param dr The Drawable whose transparency is to be applied to the region. |
| 12857 | * @param region A Region holding the current transparency information, |
| 12858 | * where any parts of the region that are set are considered to be |
| 12859 | * transparent. On return, this region will be modified to have the |
| 12860 | * transparency information reduced by the corresponding parts of the |
| 12861 | * Drawable that are not transparent. |
| 12862 | * {@hide} |
| 12863 | */ |
| 12864 | public void applyDrawableToTransparentRegion(Drawable dr, Region region) { |
| 12865 | if (DBG) { |
| 12866 | Log.i("View", "Getting transparent region for: " + this); |
| 12867 | } |
| 12868 | final Region r = dr.getTransparentRegion(); |
| 12869 | final Rect db = dr.getBounds(); |
| 12870 | final AttachInfo attachInfo = mAttachInfo; |
| 12871 | if (r != null && attachInfo != null) { |
| 12872 | final int w = getRight()-getLeft(); |
| 12873 | final int h = getBottom()-getTop(); |
| 12874 | if (db.left > 0) { |
| 12875 | //Log.i("VIEW", "Drawable left " + db.left + " > view 0"); |
| 12876 | r.op(0, 0, db.left, h, Region.Op.UNION); |
| 12877 | } |
| 12878 | if (db.right < w) { |
| 12879 | //Log.i("VIEW", "Drawable right " + db.right + " < view " + w); |
| 12880 | r.op(db.right, 0, w, h, Region.Op.UNION); |
| 12881 | } |
| 12882 | if (db.top > 0) { |
| 12883 | //Log.i("VIEW", "Drawable top " + db.top + " > view 0"); |
| 12884 | r.op(0, 0, w, db.top, Region.Op.UNION); |
| 12885 | } |
| 12886 | if (db.bottom < h) { |
| 12887 | //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h); |
| 12888 | r.op(0, db.bottom, w, h, Region.Op.UNION); |
| 12889 | } |
| 12890 | final int[] location = attachInfo.mTransparentLocation; |
| 12891 | getLocationInWindow(location); |
| 12892 | r.translate(location[0], location[1]); |
| 12893 | region.op(r, Region.Op.INTERSECT); |
| 12894 | } else { |
| 12895 | region.op(db, Region.Op.DIFFERENCE); |
| 12896 | } |
| 12897 | } |
| 12898 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 12899 | private void checkForLongClick(int delayOffset) { |
| 12900 | if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) { |
| 12901 | mHasPerformedLongPress = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12902 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 12903 | if (mPendingCheckForLongPress == null) { |
| 12904 | mPendingCheckForLongPress = new CheckForLongPress(); |
| 12905 | } |
| 12906 | mPendingCheckForLongPress.rememberWindowAttachCount(); |
| 12907 | postDelayed(mPendingCheckForLongPress, |
| 12908 | ViewConfiguration.getLongPressTimeout() - delayOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12909 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12910 | } |
| 12911 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12912 | /** |
| 12913 | * Inflate a view from an XML resource. This convenience method wraps the {@link |
| 12914 | * LayoutInflater} class, which provides a full range of options for view inflation. |
| 12915 | * |
| 12916 | * @param context The Context object for your activity or application. |
| 12917 | * @param resource The resource ID to inflate |
| 12918 | * @param root A view group that will be the parent. Used to properly inflate the |
| 12919 | * layout_* parameters. |
| 12920 | * @see LayoutInflater |
| 12921 | */ |
| 12922 | public static View inflate(Context context, int resource, ViewGroup root) { |
| 12923 | LayoutInflater factory = LayoutInflater.from(context); |
| 12924 | return factory.inflate(resource, root); |
| 12925 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 12926 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12927 | /** |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 12928 | * Scroll the view with standard behavior for scrolling beyond the normal |
| 12929 | * content boundaries. Views that call this method should override |
| 12930 | * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the |
| 12931 | * results of an over-scroll operation. |
| 12932 | * |
| 12933 | * Views can use this method to handle any touch or fling-based scrolling. |
| 12934 | * |
| 12935 | * @param deltaX Change in X in pixels |
| 12936 | * @param deltaY Change in Y in pixels |
| 12937 | * @param scrollX Current X scroll value in pixels before applying deltaX |
| 12938 | * @param scrollY Current Y scroll value in pixels before applying deltaY |
| 12939 | * @param scrollRangeX Maximum content scroll range along the X axis |
| 12940 | * @param scrollRangeY Maximum content scroll range along the Y axis |
| 12941 | * @param maxOverScrollX Number of pixels to overscroll by in either direction |
| 12942 | * along the X axis. |
| 12943 | * @param maxOverScrollY Number of pixels to overscroll by in either direction |
| 12944 | * along the Y axis. |
| 12945 | * @param isTouchEvent true if this scroll operation is the result of a touch event. |
| 12946 | * @return true if scrolling was clamped to an over-scroll boundary along either |
| 12947 | * axis, false otherwise. |
| 12948 | */ |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 12949 | @SuppressWarnings({"UnusedParameters"}) |
Adam Powell | 637d337 | 2010-08-25 14:37:03 -0700 | [diff] [blame] | 12950 | protected boolean overScrollBy(int deltaX, int deltaY, |
| 12951 | int scrollX, int scrollY, |
| 12952 | int scrollRangeX, int scrollRangeY, |
| 12953 | int maxOverScrollX, int maxOverScrollY, |
| 12954 | boolean isTouchEvent) { |
| 12955 | final int overScrollMode = mOverScrollMode; |
| 12956 | final boolean canScrollHorizontal = |
| 12957 | computeHorizontalScrollRange() > computeHorizontalScrollExtent(); |
| 12958 | final boolean canScrollVertical = |
| 12959 | computeVerticalScrollRange() > computeVerticalScrollExtent(); |
| 12960 | final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS || |
| 12961 | (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal); |
| 12962 | final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS || |
| 12963 | (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical); |
| 12964 | |
| 12965 | int newScrollX = scrollX + deltaX; |
| 12966 | if (!overScrollHorizontal) { |
| 12967 | maxOverScrollX = 0; |
| 12968 | } |
| 12969 | |
| 12970 | int newScrollY = scrollY + deltaY; |
| 12971 | if (!overScrollVertical) { |
| 12972 | maxOverScrollY = 0; |
| 12973 | } |
| 12974 | |
| 12975 | // Clamp values if at the limits and record |
| 12976 | final int left = -maxOverScrollX; |
| 12977 | final int right = maxOverScrollX + scrollRangeX; |
| 12978 | final int top = -maxOverScrollY; |
| 12979 | final int bottom = maxOverScrollY + scrollRangeY; |
| 12980 | |
| 12981 | boolean clampedX = false; |
| 12982 | if (newScrollX > right) { |
| 12983 | newScrollX = right; |
| 12984 | clampedX = true; |
| 12985 | } else if (newScrollX < left) { |
| 12986 | newScrollX = left; |
| 12987 | clampedX = true; |
| 12988 | } |
| 12989 | |
| 12990 | boolean clampedY = false; |
| 12991 | if (newScrollY > bottom) { |
| 12992 | newScrollY = bottom; |
| 12993 | clampedY = true; |
| 12994 | } else if (newScrollY < top) { |
| 12995 | newScrollY = top; |
| 12996 | clampedY = true; |
| 12997 | } |
| 12998 | |
| 12999 | onOverScrolled(newScrollX, newScrollY, clampedX, clampedY); |
| 13000 | |
| 13001 | return clampedX || clampedY; |
| 13002 | } |
| 13003 | |
| 13004 | /** |
| 13005 | * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to |
| 13006 | * respond to the results of an over-scroll operation. |
| 13007 | * |
| 13008 | * @param scrollX New X scroll value in pixels |
| 13009 | * @param scrollY New Y scroll value in pixels |
| 13010 | * @param clampedX True if scrollX was clamped to an over-scroll boundary |
| 13011 | * @param clampedY True if scrollY was clamped to an over-scroll boundary |
| 13012 | */ |
| 13013 | protected void onOverScrolled(int scrollX, int scrollY, |
| 13014 | boolean clampedX, boolean clampedY) { |
| 13015 | // Intentionally empty. |
| 13016 | } |
| 13017 | |
| 13018 | /** |
| 13019 | * Returns the over-scroll mode for this view. The result will be |
| 13020 | * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS} |
| 13021 | * (allow over-scrolling only if the view content is larger than the container), |
| 13022 | * or {@link #OVER_SCROLL_NEVER}. |
| 13023 | * |
| 13024 | * @return This view's over-scroll mode. |
| 13025 | */ |
| 13026 | public int getOverScrollMode() { |
| 13027 | return mOverScrollMode; |
| 13028 | } |
| 13029 | |
| 13030 | /** |
| 13031 | * Set the over-scroll mode for this view. Valid over-scroll modes are |
| 13032 | * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS} |
| 13033 | * (allow over-scrolling only if the view content is larger than the container), |
| 13034 | * or {@link #OVER_SCROLL_NEVER}. |
| 13035 | * |
| 13036 | * Setting the over-scroll mode of a view will have an effect only if the |
| 13037 | * view is capable of scrolling. |
| 13038 | * |
| 13039 | * @param overScrollMode The new over-scroll mode for this view. |
| 13040 | */ |
| 13041 | public void setOverScrollMode(int overScrollMode) { |
| 13042 | if (overScrollMode != OVER_SCROLL_ALWAYS && |
| 13043 | overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS && |
| 13044 | overScrollMode != OVER_SCROLL_NEVER) { |
| 13045 | throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode); |
| 13046 | } |
| 13047 | mOverScrollMode = overScrollMode; |
| 13048 | } |
| 13049 | |
| 13050 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 13051 | * Gets a scale factor that determines the distance the view should scroll |
| 13052 | * vertically in response to {@link MotionEvent#ACTION_SCROLL}. |
| 13053 | * @return The vertical scroll scale factor. |
| 13054 | * @hide |
| 13055 | */ |
| 13056 | protected float getVerticalScrollFactor() { |
| 13057 | if (mVerticalScrollFactor == 0) { |
| 13058 | TypedValue outValue = new TypedValue(); |
| 13059 | if (!mContext.getTheme().resolveAttribute( |
| 13060 | com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) { |
| 13061 | throw new IllegalStateException( |
| 13062 | "Expected theme to define listPreferredItemHeight."); |
| 13063 | } |
| 13064 | mVerticalScrollFactor = outValue.getDimension( |
| 13065 | mContext.getResources().getDisplayMetrics()); |
| 13066 | } |
| 13067 | return mVerticalScrollFactor; |
| 13068 | } |
| 13069 | |
| 13070 | /** |
| 13071 | * Gets a scale factor that determines the distance the view should scroll |
| 13072 | * horizontally in response to {@link MotionEvent#ACTION_SCROLL}. |
| 13073 | * @return The horizontal scroll scale factor. |
| 13074 | * @hide |
| 13075 | */ |
| 13076 | protected float getHorizontalScrollFactor() { |
| 13077 | // TODO: Should use something else. |
| 13078 | return getVerticalScrollFactor(); |
| 13079 | } |
| 13080 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13081 | /** |
| 13082 | * Return the value specifying the text direction or policy that was set with |
| 13083 | * {@link #setTextDirection(int)}. |
| 13084 | * |
| 13085 | * @return the defined text direction. It can be one of: |
| 13086 | * |
| 13087 | * {@link #TEXT_DIRECTION_INHERIT}, |
| 13088 | * {@link #TEXT_DIRECTION_FIRST_STRONG} |
| 13089 | * {@link #TEXT_DIRECTION_ANY_RTL}, |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 13090 | * {@link #TEXT_DIRECTION_CHAR_COUNT}, |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13091 | * {@link #TEXT_DIRECTION_LTR}, |
| 13092 | * {@link #TEXT_DIRECTION_RTL}, |
| 13093 | * |
| 13094 | * @hide |
| 13095 | */ |
| 13096 | public int getTextDirection() { |
| 13097 | return mTextDirection; |
| 13098 | } |
| 13099 | |
| 13100 | /** |
| 13101 | * Set the text direction. |
| 13102 | * |
| 13103 | * @param textDirection the direction to set. Should be one of: |
| 13104 | * |
| 13105 | * {@link #TEXT_DIRECTION_INHERIT}, |
| 13106 | * {@link #TEXT_DIRECTION_FIRST_STRONG} |
| 13107 | * {@link #TEXT_DIRECTION_ANY_RTL}, |
Fabrice Di Meglio | 8f50265 | 2011-06-29 20:40:43 -0700 | [diff] [blame] | 13108 | * {@link #TEXT_DIRECTION_CHAR_COUNT}, |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13109 | * {@link #TEXT_DIRECTION_LTR}, |
| 13110 | * {@link #TEXT_DIRECTION_RTL}, |
| 13111 | * |
| 13112 | * @hide |
| 13113 | */ |
| 13114 | public void setTextDirection(int textDirection) { |
| 13115 | if (textDirection != mTextDirection) { |
| 13116 | mTextDirection = textDirection; |
| 13117 | resetResolvedTextDirection(); |
| 13118 | requestLayout(); |
| 13119 | } |
| 13120 | } |
| 13121 | |
| 13122 | /** |
| 13123 | * Return the resolved text direction. |
| 13124 | * |
| 13125 | * @return the resolved text direction. Return one of: |
| 13126 | * |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13127 | * {@link #TEXT_DIRECTION_FIRST_STRONG} |
| 13128 | * {@link #TEXT_DIRECTION_ANY_RTL}, |
| 13129 | * {@link #TEXT_DIRECTION_CHAR_COUNT}, |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13130 | * {@link #TEXT_DIRECTION_LTR}, |
| 13131 | * {@link #TEXT_DIRECTION_RTL}, |
| 13132 | * |
| 13133 | * @hide |
| 13134 | */ |
| 13135 | public int getResolvedTextDirection() { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13136 | if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13137 | resolveTextDirection(); |
| 13138 | } |
| 13139 | return mResolvedTextDirection; |
| 13140 | } |
| 13141 | |
| 13142 | /** |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13143 | * Resolve the text direction. |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13144 | */ |
| 13145 | protected void resolveTextDirection() { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13146 | if (mTextDirection != TEXT_DIRECTION_INHERIT) { |
| 13147 | mResolvedTextDirection = mTextDirection; |
| 13148 | return; |
| 13149 | } |
| 13150 | if (mParent != null && mParent instanceof ViewGroup) { |
| 13151 | mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection(); |
| 13152 | return; |
| 13153 | } |
| 13154 | mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13155 | } |
| 13156 | |
| 13157 | /** |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13158 | * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection(). |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13159 | */ |
| 13160 | protected void resetResolvedTextDirection() { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 13161 | mResolvedTextDirection = TEXT_DIRECTION_INHERIT; |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 13162 | } |
| 13163 | |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 13164 | // |
| 13165 | // Properties |
| 13166 | // |
| 13167 | /** |
| 13168 | * A Property wrapper around the <code>alpha</code> functionality handled by the |
| 13169 | * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods. |
| 13170 | */ |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 13171 | public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") { |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 13172 | @Override |
| 13173 | public void setValue(View object, float value) { |
| 13174 | object.setAlpha(value); |
| 13175 | } |
| 13176 | |
| 13177 | @Override |
| 13178 | public Float get(View object) { |
| 13179 | return object.getAlpha(); |
| 13180 | } |
| 13181 | }; |
| 13182 | |
| 13183 | /** |
| 13184 | * A Property wrapper around the <code>translationX</code> functionality handled by the |
| 13185 | * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods. |
| 13186 | */ |
| 13187 | public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") { |
| 13188 | @Override |
| 13189 | public void setValue(View object, float value) { |
| 13190 | object.setTranslationX(value); |
| 13191 | } |
| 13192 | |
| 13193 | @Override |
| 13194 | public Float get(View object) { |
| 13195 | return object.getTranslationX(); |
| 13196 | } |
| 13197 | }; |
| 13198 | |
| 13199 | /** |
| 13200 | * A Property wrapper around the <code>translationY</code> functionality handled by the |
| 13201 | * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods. |
| 13202 | */ |
| 13203 | public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") { |
| 13204 | @Override |
| 13205 | public void setValue(View object, float value) { |
| 13206 | object.setTranslationY(value); |
| 13207 | } |
| 13208 | |
| 13209 | @Override |
| 13210 | public Float get(View object) { |
| 13211 | return object.getTranslationY(); |
| 13212 | } |
| 13213 | }; |
| 13214 | |
| 13215 | /** |
| 13216 | * A Property wrapper around the <code>x</code> functionality handled by the |
| 13217 | * {@link View#setX(float)} and {@link View#getX()} methods. |
| 13218 | */ |
| 13219 | public static Property<View, Float> X = new FloatProperty<View>("x") { |
| 13220 | @Override |
| 13221 | public void setValue(View object, float value) { |
| 13222 | object.setX(value); |
| 13223 | } |
| 13224 | |
| 13225 | @Override |
| 13226 | public Float get(View object) { |
| 13227 | return object.getX(); |
| 13228 | } |
| 13229 | }; |
| 13230 | |
| 13231 | /** |
| 13232 | * A Property wrapper around the <code>y</code> functionality handled by the |
| 13233 | * {@link View#setY(float)} and {@link View#getY()} methods. |
| 13234 | */ |
| 13235 | public static Property<View, Float> Y = new FloatProperty<View>("y") { |
| 13236 | @Override |
| 13237 | public void setValue(View object, float value) { |
| 13238 | object.setY(value); |
| 13239 | } |
| 13240 | |
| 13241 | @Override |
| 13242 | public Float get(View object) { |
| 13243 | return object.getY(); |
| 13244 | } |
| 13245 | }; |
| 13246 | |
| 13247 | /** |
| 13248 | * A Property wrapper around the <code>rotation</code> functionality handled by the |
| 13249 | * {@link View#setRotation(float)} and {@link View#getRotation()} methods. |
| 13250 | */ |
| 13251 | public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") { |
| 13252 | @Override |
| 13253 | public void setValue(View object, float value) { |
| 13254 | object.setRotation(value); |
| 13255 | } |
| 13256 | |
| 13257 | @Override |
| 13258 | public Float get(View object) { |
| 13259 | return object.getRotation(); |
| 13260 | } |
| 13261 | }; |
| 13262 | |
| 13263 | /** |
| 13264 | * A Property wrapper around the <code>rotationX</code> functionality handled by the |
| 13265 | * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods. |
| 13266 | */ |
| 13267 | public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") { |
| 13268 | @Override |
| 13269 | public void setValue(View object, float value) { |
| 13270 | object.setRotationX(value); |
| 13271 | } |
| 13272 | |
| 13273 | @Override |
| 13274 | public Float get(View object) { |
| 13275 | return object.getRotationX(); |
| 13276 | } |
| 13277 | }; |
| 13278 | |
| 13279 | /** |
| 13280 | * A Property wrapper around the <code>rotationY</code> functionality handled by the |
| 13281 | * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods. |
| 13282 | */ |
| 13283 | public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") { |
| 13284 | @Override |
| 13285 | public void setValue(View object, float value) { |
| 13286 | object.setRotationY(value); |
| 13287 | } |
| 13288 | |
| 13289 | @Override |
| 13290 | public Float get(View object) { |
| 13291 | return object.getRotationY(); |
| 13292 | } |
| 13293 | }; |
| 13294 | |
| 13295 | /** |
| 13296 | * A Property wrapper around the <code>scaleX</code> functionality handled by the |
| 13297 | * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods. |
| 13298 | */ |
| 13299 | public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") { |
| 13300 | @Override |
| 13301 | public void setValue(View object, float value) { |
| 13302 | object.setScaleX(value); |
| 13303 | } |
| 13304 | |
| 13305 | @Override |
| 13306 | public Float get(View object) { |
| 13307 | return object.getScaleX(); |
| 13308 | } |
| 13309 | }; |
| 13310 | |
| 13311 | /** |
| 13312 | * A Property wrapper around the <code>scaleY</code> functionality handled by the |
| 13313 | * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods. |
| 13314 | */ |
| 13315 | public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") { |
| 13316 | @Override |
| 13317 | public void setValue(View object, float value) { |
| 13318 | object.setScaleY(value); |
| 13319 | } |
| 13320 | |
| 13321 | @Override |
| 13322 | public Float get(View object) { |
| 13323 | return object.getScaleY(); |
| 13324 | } |
| 13325 | }; |
| 13326 | |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 13327 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13328 | * A MeasureSpec encapsulates the layout requirements passed from parent to child. |
| 13329 | * Each MeasureSpec represents a requirement for either the width or the height. |
| 13330 | * A MeasureSpec is comprised of a size and a mode. There are three possible |
| 13331 | * modes: |
| 13332 | * <dl> |
| 13333 | * <dt>UNSPECIFIED</dt> |
| 13334 | * <dd> |
| 13335 | * The parent has not imposed any constraint on the child. It can be whatever size |
| 13336 | * it wants. |
| 13337 | * </dd> |
| 13338 | * |
| 13339 | * <dt>EXACTLY</dt> |
| 13340 | * <dd> |
| 13341 | * The parent has determined an exact size for the child. The child is going to be |
| 13342 | * given those bounds regardless of how big it wants to be. |
| 13343 | * </dd> |
| 13344 | * |
| 13345 | * <dt>AT_MOST</dt> |
| 13346 | * <dd> |
| 13347 | * The child can be as large as it wants up to the specified size. |
| 13348 | * </dd> |
| 13349 | * </dl> |
| 13350 | * |
| 13351 | * MeasureSpecs are implemented as ints to reduce object allocation. This class |
| 13352 | * is provided to pack and unpack the <size, mode> tuple into the int. |
| 13353 | */ |
| 13354 | public static class MeasureSpec { |
| 13355 | private static final int MODE_SHIFT = 30; |
| 13356 | private static final int MODE_MASK = 0x3 << MODE_SHIFT; |
| 13357 | |
| 13358 | /** |
| 13359 | * Measure specification mode: The parent has not imposed any constraint |
| 13360 | * on the child. It can be whatever size it wants. |
| 13361 | */ |
| 13362 | public static final int UNSPECIFIED = 0 << MODE_SHIFT; |
| 13363 | |
| 13364 | /** |
| 13365 | * Measure specification mode: The parent has determined an exact size |
| 13366 | * for the child. The child is going to be given those bounds regardless |
| 13367 | * of how big it wants to be. |
| 13368 | */ |
| 13369 | public static final int EXACTLY = 1 << MODE_SHIFT; |
| 13370 | |
| 13371 | /** |
| 13372 | * Measure specification mode: The child can be as large as it wants up |
| 13373 | * to the specified size. |
| 13374 | */ |
| 13375 | public static final int AT_MOST = 2 << MODE_SHIFT; |
| 13376 | |
| 13377 | /** |
| 13378 | * Creates a measure specification based on the supplied size and mode. |
| 13379 | * |
| 13380 | * The mode must always be one of the following: |
| 13381 | * <ul> |
| 13382 | * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li> |
| 13383 | * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li> |
| 13384 | * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li> |
| 13385 | * </ul> |
| 13386 | * |
| 13387 | * @param size the size of the measure specification |
| 13388 | * @param mode the mode of the measure specification |
| 13389 | * @return the measure specification based on size and mode |
| 13390 | */ |
| 13391 | public static int makeMeasureSpec(int size, int mode) { |
| 13392 | return size + mode; |
| 13393 | } |
| 13394 | |
| 13395 | /** |
| 13396 | * Extracts the mode from the supplied measure specification. |
| 13397 | * |
| 13398 | * @param measureSpec the measure specification to extract the mode from |
| 13399 | * @return {@link android.view.View.MeasureSpec#UNSPECIFIED}, |
| 13400 | * {@link android.view.View.MeasureSpec#AT_MOST} or |
| 13401 | * {@link android.view.View.MeasureSpec#EXACTLY} |
| 13402 | */ |
| 13403 | public static int getMode(int measureSpec) { |
| 13404 | return (measureSpec & MODE_MASK); |
| 13405 | } |
| 13406 | |
| 13407 | /** |
| 13408 | * Extracts the size from the supplied measure specification. |
| 13409 | * |
| 13410 | * @param measureSpec the measure specification to extract the size from |
| 13411 | * @return the size in pixels defined in the supplied measure specification |
| 13412 | */ |
| 13413 | public static int getSize(int measureSpec) { |
| 13414 | return (measureSpec & ~MODE_MASK); |
| 13415 | } |
| 13416 | |
| 13417 | /** |
| 13418 | * Returns a String representation of the specified measure |
| 13419 | * specification. |
| 13420 | * |
| 13421 | * @param measureSpec the measure specification to convert to a String |
| 13422 | * @return a String with the following format: "MeasureSpec: MODE SIZE" |
| 13423 | */ |
| 13424 | public static String toString(int measureSpec) { |
| 13425 | int mode = getMode(measureSpec); |
| 13426 | int size = getSize(measureSpec); |
| 13427 | |
| 13428 | StringBuilder sb = new StringBuilder("MeasureSpec: "); |
| 13429 | |
| 13430 | if (mode == UNSPECIFIED) |
| 13431 | sb.append("UNSPECIFIED "); |
| 13432 | else if (mode == EXACTLY) |
| 13433 | sb.append("EXACTLY "); |
| 13434 | else if (mode == AT_MOST) |
| 13435 | sb.append("AT_MOST "); |
| 13436 | else |
| 13437 | sb.append(mode).append(" "); |
| 13438 | |
| 13439 | sb.append(size); |
| 13440 | return sb.toString(); |
| 13441 | } |
| 13442 | } |
| 13443 | |
| 13444 | class CheckForLongPress implements Runnable { |
| 13445 | |
| 13446 | private int mOriginalWindowAttachCount; |
| 13447 | |
| 13448 | public void run() { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 13449 | if (isPressed() && (mParent != null) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13450 | && mOriginalWindowAttachCount == mWindowAttachCount) { |
| 13451 | if (performLongClick()) { |
| 13452 | mHasPerformedLongPress = true; |
| 13453 | } |
| 13454 | } |
| 13455 | } |
| 13456 | |
| 13457 | public void rememberWindowAttachCount() { |
| 13458 | mOriginalWindowAttachCount = mWindowAttachCount; |
| 13459 | } |
| 13460 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 13461 | |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 13462 | private final class CheckForTap implements Runnable { |
| 13463 | public void run() { |
| 13464 | mPrivateFlags &= ~PREPRESSED; |
| 13465 | mPrivateFlags |= PRESSED; |
| 13466 | refreshDrawableState(); |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 13467 | checkForLongClick(ViewConfiguration.getTapTimeout()); |
Adam Powell | e14579b | 2009-12-16 18:39:52 -0800 | [diff] [blame] | 13468 | } |
| 13469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13470 | |
Adam Powell | a35d768 | 2010-03-12 14:48:13 -0800 | [diff] [blame] | 13471 | private final class PerformClick implements Runnable { |
| 13472 | public void run() { |
| 13473 | performClick(); |
| 13474 | } |
| 13475 | } |
| 13476 | |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 13477 | /** @hide */ |
| 13478 | public void hackTurnOffWindowResizeAnim(boolean off) { |
| 13479 | mAttachInfo.mTurnOffWindowResizeAnim = off; |
| 13480 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 13481 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13482 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 13483 | * This method returns a ViewPropertyAnimator object, which can be used to animate |
| 13484 | * specific properties on this View. |
| 13485 | * |
| 13486 | * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View. |
| 13487 | */ |
| 13488 | public ViewPropertyAnimator animate() { |
| 13489 | if (mAnimator == null) { |
| 13490 | mAnimator = new ViewPropertyAnimator(this); |
| 13491 | } |
| 13492 | return mAnimator; |
| 13493 | } |
| 13494 | |
| 13495 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13496 | * Interface definition for a callback to be invoked when a key event is |
| 13497 | * dispatched to this view. The callback will be invoked before the key |
| 13498 | * event is given to the view. |
| 13499 | */ |
| 13500 | public interface OnKeyListener { |
| 13501 | /** |
| 13502 | * Called when a key is dispatched to a view. This allows listeners to |
| 13503 | * get a chance to respond before the target view. |
| 13504 | * |
| 13505 | * @param v The view the key has been dispatched to. |
| 13506 | * @param keyCode The code for the physical key that was pressed |
| 13507 | * @param event The KeyEvent object containing full information about |
| 13508 | * the event. |
| 13509 | * @return True if the listener has consumed the event, false otherwise. |
| 13510 | */ |
| 13511 | boolean onKey(View v, int keyCode, KeyEvent event); |
| 13512 | } |
| 13513 | |
| 13514 | /** |
| 13515 | * Interface definition for a callback to be invoked when a touch event is |
| 13516 | * dispatched to this view. The callback will be invoked before the touch |
| 13517 | * event is given to the view. |
| 13518 | */ |
| 13519 | public interface OnTouchListener { |
| 13520 | /** |
| 13521 | * Called when a touch event is dispatched to a view. This allows listeners to |
| 13522 | * get a chance to respond before the target view. |
| 13523 | * |
| 13524 | * @param v The view the touch event has been dispatched to. |
| 13525 | * @param event The MotionEvent object containing full information about |
| 13526 | * the event. |
| 13527 | * @return True if the listener has consumed the event, false otherwise. |
| 13528 | */ |
| 13529 | boolean onTouch(View v, MotionEvent event); |
| 13530 | } |
| 13531 | |
| 13532 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 13533 | * Interface definition for a callback to be invoked when a hover event is |
| 13534 | * dispatched to this view. The callback will be invoked before the hover |
| 13535 | * event is given to the view. |
| 13536 | */ |
| 13537 | public interface OnHoverListener { |
| 13538 | /** |
| 13539 | * Called when a hover event is dispatched to a view. This allows listeners to |
| 13540 | * get a chance to respond before the target view. |
| 13541 | * |
| 13542 | * @param v The view the hover event has been dispatched to. |
| 13543 | * @param event The MotionEvent object containing full information about |
| 13544 | * the event. |
| 13545 | * @return True if the listener has consumed the event, false otherwise. |
| 13546 | */ |
| 13547 | boolean onHover(View v, MotionEvent event); |
| 13548 | } |
| 13549 | |
| 13550 | /** |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 13551 | * Interface definition for a callback to be invoked when a generic motion event is |
| 13552 | * dispatched to this view. The callback will be invoked before the generic motion |
| 13553 | * event is given to the view. |
| 13554 | */ |
| 13555 | public interface OnGenericMotionListener { |
| 13556 | /** |
| 13557 | * Called when a generic motion event is dispatched to a view. This allows listeners to |
| 13558 | * get a chance to respond before the target view. |
| 13559 | * |
| 13560 | * @param v The view the generic motion event has been dispatched to. |
| 13561 | * @param event The MotionEvent object containing full information about |
| 13562 | * the event. |
| 13563 | * @return True if the listener has consumed the event, false otherwise. |
| 13564 | */ |
| 13565 | boolean onGenericMotion(View v, MotionEvent event); |
| 13566 | } |
| 13567 | |
| 13568 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13569 | * Interface definition for a callback to be invoked when a view has been clicked and held. |
| 13570 | */ |
| 13571 | public interface OnLongClickListener { |
| 13572 | /** |
| 13573 | * Called when a view has been clicked and held. |
| 13574 | * |
| 13575 | * @param v The view that was clicked and held. |
| 13576 | * |
Brad Fitzpatrick | 69ea4e1 | 2011-01-05 11:13:40 -0800 | [diff] [blame] | 13577 | * @return true if the callback consumed the long click, false otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13578 | */ |
| 13579 | boolean onLongClick(View v); |
| 13580 | } |
| 13581 | |
| 13582 | /** |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 13583 | * Interface definition for a callback to be invoked when a drag is being dispatched |
| 13584 | * to this view. The callback will be invoked before the hosting view's own |
| 13585 | * onDrag(event) method. If the listener wants to fall back to the hosting view's |
| 13586 | * onDrag(event) behavior, it should return 'false' from this callback. |
| 13587 | */ |
| 13588 | public interface OnDragListener { |
| 13589 | /** |
| 13590 | * Called when a drag event is dispatched to a view. This allows listeners |
| 13591 | * to get a chance to override base View behavior. |
| 13592 | * |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 13593 | * @param v The View that received the drag event. |
| 13594 | * @param event The {@link android.view.DragEvent} object for the drag event. |
| 13595 | * @return {@code true} if the drag event was handled successfully, or {@code false} |
| 13596 | * if the drag event was not handled. Note that {@code false} will trigger the View |
| 13597 | * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler. |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 13598 | */ |
| 13599 | boolean onDrag(View v, DragEvent event); |
| 13600 | } |
| 13601 | |
| 13602 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13603 | * Interface definition for a callback to be invoked when the focus state of |
| 13604 | * a view changed. |
| 13605 | */ |
| 13606 | public interface OnFocusChangeListener { |
| 13607 | /** |
| 13608 | * Called when the focus state of a view has changed. |
| 13609 | * |
| 13610 | * @param v The view whose state has changed. |
| 13611 | * @param hasFocus The new focus state of v. |
| 13612 | */ |
| 13613 | void onFocusChange(View v, boolean hasFocus); |
| 13614 | } |
| 13615 | |
| 13616 | /** |
| 13617 | * Interface definition for a callback to be invoked when a view is clicked. |
| 13618 | */ |
| 13619 | public interface OnClickListener { |
| 13620 | /** |
| 13621 | * Called when a view has been clicked. |
| 13622 | * |
| 13623 | * @param v The view that was clicked. |
| 13624 | */ |
| 13625 | void onClick(View v); |
| 13626 | } |
| 13627 | |
| 13628 | /** |
| 13629 | * Interface definition for a callback to be invoked when the context menu |
| 13630 | * for this view is being built. |
| 13631 | */ |
| 13632 | public interface OnCreateContextMenuListener { |
| 13633 | /** |
| 13634 | * Called when the context menu for this view is being built. It is not |
| 13635 | * safe to hold onto the menu after this method returns. |
| 13636 | * |
| 13637 | * @param menu The context menu that is being built |
| 13638 | * @param v The view for which the context menu is being built |
| 13639 | * @param menuInfo Extra information about the item for which the |
| 13640 | * context menu should be shown. This information will vary |
| 13641 | * depending on the class of v. |
| 13642 | */ |
| 13643 | void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo); |
| 13644 | } |
| 13645 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 13646 | /** |
| 13647 | * Interface definition for a callback to be invoked when the status bar changes |
| 13648 | * visibility. |
| 13649 | * |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 13650 | * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener) |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 13651 | */ |
| 13652 | public interface OnSystemUiVisibilityChangeListener { |
| 13653 | /** |
| 13654 | * Called when the status bar changes visibility because of a call to |
Romain Guy | 5c22a8c | 2011-05-13 11:48:45 -0700 | [diff] [blame] | 13655 | * {@link View#setSystemUiVisibility(int)}. |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 13656 | * |
| 13657 | * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}. |
| 13658 | */ |
| 13659 | public void onSystemUiVisibilityChange(int visibility); |
| 13660 | } |
| 13661 | |
Adam Powell | 4afd62b | 2011-02-18 15:02:18 -0800 | [diff] [blame] | 13662 | /** |
| 13663 | * Interface definition for a callback to be invoked when this view is attached |
| 13664 | * or detached from its window. |
| 13665 | */ |
| 13666 | public interface OnAttachStateChangeListener { |
| 13667 | /** |
| 13668 | * Called when the view is attached to a window. |
| 13669 | * @param v The view that was attached |
| 13670 | */ |
| 13671 | public void onViewAttachedToWindow(View v); |
| 13672 | /** |
| 13673 | * Called when the view is detached from a window. |
| 13674 | * @param v The view that was detached |
| 13675 | */ |
| 13676 | public void onViewDetachedFromWindow(View v); |
| 13677 | } |
| 13678 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13679 | private final class UnsetPressedState implements Runnable { |
| 13680 | public void run() { |
| 13681 | setPressed(false); |
| 13682 | } |
| 13683 | } |
| 13684 | |
| 13685 | /** |
| 13686 | * Base class for derived classes that want to save and restore their own |
| 13687 | * state in {@link android.view.View#onSaveInstanceState()}. |
| 13688 | */ |
| 13689 | public static class BaseSavedState extends AbsSavedState { |
| 13690 | /** |
| 13691 | * Constructor used when reading from a parcel. Reads the state of the superclass. |
| 13692 | * |
| 13693 | * @param source |
| 13694 | */ |
| 13695 | public BaseSavedState(Parcel source) { |
| 13696 | super(source); |
| 13697 | } |
| 13698 | |
| 13699 | /** |
| 13700 | * Constructor called by derived classes when creating their SavedState objects |
| 13701 | * |
| 13702 | * @param superState The state of the superclass of this view |
| 13703 | */ |
| 13704 | public BaseSavedState(Parcelable superState) { |
| 13705 | super(superState); |
| 13706 | } |
| 13707 | |
| 13708 | public static final Parcelable.Creator<BaseSavedState> CREATOR = |
| 13709 | new Parcelable.Creator<BaseSavedState>() { |
| 13710 | public BaseSavedState createFromParcel(Parcel in) { |
| 13711 | return new BaseSavedState(in); |
| 13712 | } |
| 13713 | |
| 13714 | public BaseSavedState[] newArray(int size) { |
| 13715 | return new BaseSavedState[size]; |
| 13716 | } |
| 13717 | }; |
| 13718 | } |
| 13719 | |
| 13720 | /** |
| 13721 | * A set of information given to a view when it is attached to its parent |
| 13722 | * window. |
| 13723 | */ |
| 13724 | static class AttachInfo { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13725 | interface Callbacks { |
| 13726 | void playSoundEffect(int effectId); |
| 13727 | boolean performHapticFeedback(int effectId, boolean always); |
| 13728 | } |
| 13729 | |
| 13730 | /** |
| 13731 | * InvalidateInfo is used to post invalidate(int, int, int, int) messages |
| 13732 | * to a Handler. This class contains the target (View) to invalidate and |
| 13733 | * the coordinates of the dirty rectangle. |
| 13734 | * |
| 13735 | * For performance purposes, this class also implements a pool of up to |
| 13736 | * POOL_LIMIT objects that get reused. This reduces memory allocations |
| 13737 | * whenever possible. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13738 | */ |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13739 | static class InvalidateInfo implements Poolable<InvalidateInfo> { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13740 | private static final int POOL_LIMIT = 10; |
Romain Guy | 2e9bbce | 2009-04-01 10:40:10 -0700 | [diff] [blame] | 13741 | private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool( |
| 13742 | Pools.finitePool(new PoolableManager<InvalidateInfo>() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13743 | public InvalidateInfo newInstance() { |
| 13744 | return new InvalidateInfo(); |
| 13745 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13746 | |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13747 | public void onAcquired(InvalidateInfo element) { |
| 13748 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13749 | |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13750 | public void onReleased(InvalidateInfo element) { |
| 13751 | } |
| 13752 | }, POOL_LIMIT) |
| 13753 | ); |
| 13754 | |
| 13755 | private InvalidateInfo mNext; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 13756 | private boolean mIsPooled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13757 | |
| 13758 | View target; |
| 13759 | |
| 13760 | int left; |
| 13761 | int top; |
| 13762 | int right; |
| 13763 | int bottom; |
| 13764 | |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13765 | public void setNextPoolable(InvalidateInfo element) { |
| 13766 | mNext = element; |
| 13767 | } |
| 13768 | |
| 13769 | public InvalidateInfo getNextPoolable() { |
| 13770 | return mNext; |
| 13771 | } |
| 13772 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13773 | static InvalidateInfo acquire() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13774 | return sPool.acquire(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13775 | } |
| 13776 | |
| 13777 | void release() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 13778 | sPool.release(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13779 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 13780 | |
| 13781 | public boolean isPooled() { |
| 13782 | return mIsPooled; |
| 13783 | } |
| 13784 | |
| 13785 | public void setPooled(boolean isPooled) { |
| 13786 | mIsPooled = isPooled; |
| 13787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13788 | } |
| 13789 | |
| 13790 | final IWindowSession mSession; |
| 13791 | |
| 13792 | final IWindow mWindow; |
| 13793 | |
| 13794 | final IBinder mWindowToken; |
| 13795 | |
| 13796 | final Callbacks mRootCallbacks; |
| 13797 | |
Romain Guy | 59a12ca | 2011-06-09 17:48:21 -0700 | [diff] [blame] | 13798 | HardwareCanvas mHardwareCanvas; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 13799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13800 | /** |
| 13801 | * The top view of the hierarchy. |
| 13802 | */ |
| 13803 | View mRootView; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 13804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13805 | IBinder mPanelParentWindowToken; |
| 13806 | Surface mSurface; |
| 13807 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 13808 | boolean mHardwareAccelerated; |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 13809 | boolean mHardwareAccelerationRequested; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 13810 | HardwareRenderer mHardwareRenderer; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 13811 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13812 | /** |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 13813 | * Scale factor used by the compatibility mode |
| 13814 | */ |
| 13815 | float mApplicationScale; |
| 13816 | |
| 13817 | /** |
| 13818 | * Indicates whether the application is in compatibility mode |
| 13819 | */ |
| 13820 | boolean mScalingRequired; |
| 13821 | |
| 13822 | /** |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 13823 | * If set, ViewAncestor doesn't use its lame animation for when the window resizes. |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 13824 | */ |
| 13825 | boolean mTurnOffWindowResizeAnim; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 13826 | |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 13827 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13828 | * Left position of this view's window |
| 13829 | */ |
| 13830 | int mWindowLeft; |
| 13831 | |
| 13832 | /** |
| 13833 | * Top position of this view's window |
| 13834 | */ |
| 13835 | int mWindowTop; |
| 13836 | |
| 13837 | /** |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 13838 | * Indicates whether views need to use 32-bit drawing caches |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 13839 | */ |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 13840 | boolean mUse32BitDrawingCache; |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 13841 | |
| 13842 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13843 | * For windows that are full-screen but using insets to layout inside |
| 13844 | * of the screen decorations, these are the current insets for the |
| 13845 | * content of the window. |
| 13846 | */ |
| 13847 | final Rect mContentInsets = new Rect(); |
| 13848 | |
| 13849 | /** |
| 13850 | * For windows that are full-screen but using insets to layout inside |
| 13851 | * of the screen decorations, these are the current insets for the |
| 13852 | * actual visible parts of the window. |
| 13853 | */ |
| 13854 | final Rect mVisibleInsets = new Rect(); |
| 13855 | |
| 13856 | /** |
| 13857 | * The internal insets given by this window. This value is |
| 13858 | * supplied by the client (through |
| 13859 | * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will |
| 13860 | * be given to the window manager when changed to be used in laying |
| 13861 | * out windows behind it. |
| 13862 | */ |
| 13863 | final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets |
| 13864 | = new ViewTreeObserver.InternalInsetsInfo(); |
| 13865 | |
| 13866 | /** |
| 13867 | * All views in the window's hierarchy that serve as scroll containers, |
| 13868 | * used to determine if the window can be resized or must be panned |
| 13869 | * to adjust for a soft input area. |
| 13870 | */ |
| 13871 | final ArrayList<View> mScrollContainers = new ArrayList<View>(); |
| 13872 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 13873 | final KeyEvent.DispatcherState mKeyDispatchState |
| 13874 | = new KeyEvent.DispatcherState(); |
| 13875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13876 | /** |
| 13877 | * Indicates whether the view's window currently has the focus. |
| 13878 | */ |
| 13879 | boolean mHasWindowFocus; |
| 13880 | |
| 13881 | /** |
| 13882 | * The current visibility of the window. |
| 13883 | */ |
| 13884 | int mWindowVisibility; |
| 13885 | |
| 13886 | /** |
| 13887 | * Indicates the time at which drawing started to occur. |
| 13888 | */ |
| 13889 | long mDrawingTime; |
| 13890 | |
| 13891 | /** |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 13892 | * Indicates whether or not ignoring the DIRTY_MASK flags. |
| 13893 | */ |
| 13894 | boolean mIgnoreDirtyState; |
| 13895 | |
| 13896 | /** |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 13897 | * This flag tracks when the mIgnoreDirtyState flag is set during draw(), |
| 13898 | * to avoid clearing that flag prematurely. |
| 13899 | */ |
| 13900 | boolean mSetIgnoreDirtyState = false; |
| 13901 | |
| 13902 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13903 | * Indicates whether the view's window is currently in touch mode. |
| 13904 | */ |
| 13905 | boolean mInTouchMode; |
| 13906 | |
| 13907 | /** |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 13908 | * Indicates that ViewAncestor should trigger a global layout change |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13909 | * the next time it performs a traversal |
| 13910 | */ |
| 13911 | boolean mRecomputeGlobalAttributes; |
| 13912 | |
| 13913 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13914 | * Set during a traveral if any views want to keep the screen on. |
| 13915 | */ |
| 13916 | boolean mKeepScreenOn; |
| 13917 | |
| 13918 | /** |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 13919 | * Bitwise-or of all of the values that views have passed to setSystemUiVisibility(). |
| 13920 | */ |
| 13921 | int mSystemUiVisibility; |
| 13922 | |
| 13923 | /** |
| 13924 | * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener |
| 13925 | * attached. |
| 13926 | */ |
| 13927 | boolean mHasSystemUiListeners; |
| 13928 | |
| 13929 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13930 | * Set if the visibility of any views has changed. |
| 13931 | */ |
| 13932 | boolean mViewVisibilityChanged; |
| 13933 | |
| 13934 | /** |
| 13935 | * Set to true if a view has been scrolled. |
| 13936 | */ |
| 13937 | boolean mViewScrollChanged; |
| 13938 | |
| 13939 | /** |
| 13940 | * Global to the view hierarchy used as a temporary for dealing with |
| 13941 | * x/y points in the transparent region computations. |
| 13942 | */ |
| 13943 | final int[] mTransparentLocation = new int[2]; |
| 13944 | |
| 13945 | /** |
| 13946 | * Global to the view hierarchy used as a temporary for dealing with |
| 13947 | * x/y points in the ViewGroup.invalidateChild implementation. |
| 13948 | */ |
| 13949 | final int[] mInvalidateChildLocation = new int[2]; |
| 13950 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 13951 | |
| 13952 | /** |
| 13953 | * Global to the view hierarchy used as a temporary for dealing with |
| 13954 | * x/y location when view is transformed. |
| 13955 | */ |
| 13956 | final float[] mTmpTransformLocation = new float[2]; |
| 13957 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13958 | /** |
| 13959 | * The view tree observer used to dispatch global events like |
| 13960 | * layout, pre-draw, touch mode change, etc. |
| 13961 | */ |
| 13962 | final ViewTreeObserver mTreeObserver = new ViewTreeObserver(); |
| 13963 | |
| 13964 | /** |
| 13965 | * A Canvas used by the view hierarchy to perform bitmap caching. |
| 13966 | */ |
| 13967 | Canvas mCanvas; |
| 13968 | |
| 13969 | /** |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 13970 | * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13971 | * handler can be used to pump events in the UI events queue. |
| 13972 | */ |
| 13973 | final Handler mHandler; |
| 13974 | |
| 13975 | /** |
| 13976 | * Identifier for messages requesting the view to be invalidated. |
| 13977 | * Such messages should be sent to {@link #mHandler}. |
| 13978 | */ |
| 13979 | static final int INVALIDATE_MSG = 0x1; |
| 13980 | |
| 13981 | /** |
| 13982 | * Identifier for messages requesting the view to invalidate a region. |
| 13983 | * Such messages should be sent to {@link #mHandler}. |
| 13984 | */ |
| 13985 | static final int INVALIDATE_RECT_MSG = 0x2; |
| 13986 | |
| 13987 | /** |
| 13988 | * Temporary for use in computing invalidate rectangles while |
| 13989 | * calling up the hierarchy. |
| 13990 | */ |
| 13991 | final Rect mTmpInvalRect = new Rect(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 13992 | |
| 13993 | /** |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 13994 | * Temporary for use in computing hit areas with transformed views |
| 13995 | */ |
| 13996 | final RectF mTmpTransformRect = new RectF(); |
| 13997 | |
| 13998 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 13999 | * Temporary list for use in collecting focusable descendents of a view. |
| 14000 | */ |
| 14001 | final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24); |
| 14002 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14003 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 14004 | * The id of the window for accessibility purposes. |
| 14005 | */ |
| 14006 | int mAccessibilityWindowId = View.NO_ID; |
| 14007 | |
| 14008 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14009 | * Creates a new set of attachment information with the specified |
| 14010 | * events handler and thread. |
| 14011 | * |
| 14012 | * @param handler the events handler the view must use |
| 14013 | */ |
| 14014 | AttachInfo(IWindowSession session, IWindow window, |
| 14015 | Handler handler, Callbacks effectPlayer) { |
| 14016 | mSession = session; |
| 14017 | mWindow = window; |
| 14018 | mWindowToken = window.asBinder(); |
| 14019 | mHandler = handler; |
| 14020 | mRootCallbacks = effectPlayer; |
| 14021 | } |
| 14022 | } |
| 14023 | |
| 14024 | /** |
| 14025 | * <p>ScrollabilityCache holds various fields used by a View when scrolling |
| 14026 | * is supported. This avoids keeping too many unused fields in most |
| 14027 | * instances of View.</p> |
| 14028 | */ |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14029 | private static class ScrollabilityCache implements Runnable { |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 14030 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14031 | /** |
| 14032 | * Scrollbars are not visible |
| 14033 | */ |
| 14034 | public static final int OFF = 0; |
| 14035 | |
| 14036 | /** |
| 14037 | * Scrollbars are visible |
| 14038 | */ |
| 14039 | public static final int ON = 1; |
| 14040 | |
| 14041 | /** |
| 14042 | * Scrollbars are fading away |
| 14043 | */ |
| 14044 | public static final int FADING = 2; |
| 14045 | |
| 14046 | public boolean fadeScrollBars; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 14047 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14048 | public int fadingEdgeLength; |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14049 | public int scrollBarDefaultDelayBeforeFade; |
| 14050 | public int scrollBarFadeDuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14051 | |
| 14052 | public int scrollBarSize; |
| 14053 | public ScrollBarDrawable scrollBar; |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14054 | public float[] interpolatorValues; |
| 14055 | public View host; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14056 | |
| 14057 | public final Paint paint; |
| 14058 | public final Matrix matrix; |
| 14059 | public Shader shader; |
| 14060 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14061 | public final Interpolator scrollBarInterpolator = new Interpolator(1, 2); |
| 14062 | |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 14063 | private static final float[] OPAQUE = { 255 }; |
| 14064 | private static final float[] TRANSPARENT = { 0.0f }; |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 14065 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14066 | /** |
| 14067 | * When fading should start. This time moves into the future every time |
| 14068 | * a new scroll happens. Measured based on SystemClock.uptimeMillis() |
| 14069 | */ |
| 14070 | public long fadeStartTime; |
| 14071 | |
| 14072 | |
| 14073 | /** |
| 14074 | * The current state of the scrollbars: ON, OFF, or FADING |
| 14075 | */ |
| 14076 | public int state = OFF; |
| 14077 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14078 | private int mLastColor; |
| 14079 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14080 | public ScrollabilityCache(ViewConfiguration configuration, View host) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14081 | fadingEdgeLength = configuration.getScaledFadingEdgeLength(); |
| 14082 | scrollBarSize = configuration.getScaledScrollBarSize(); |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 14083 | scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay(); |
| 14084 | scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14085 | |
| 14086 | paint = new Paint(); |
| 14087 | matrix = new Matrix(); |
| 14088 | // use use a height of 1, and then wack the matrix each time we |
| 14089 | // actually use it. |
| 14090 | shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 14091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14092 | paint.setShader(shader); |
| 14093 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14094 | this.host = host; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14095 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 14096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14097 | public void setFadeColor(int color) { |
| 14098 | if (color != 0 && color != mLastColor) { |
| 14099 | mLastColor = color; |
| 14100 | color |= 0xFF000000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 14101 | |
Romain Guy | e55e1a7 | 2009-08-27 10:42:26 -0700 | [diff] [blame] | 14102 | shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000, |
| 14103 | color & 0x00FFFFFF, Shader.TileMode.CLAMP); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 14104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14105 | paint.setShader(shader); |
| 14106 | // Restore the default transfer mode (src_over) |
| 14107 | paint.setXfermode(null); |
| 14108 | } |
| 14109 | } |
Joe Malin | 32736f0 | 2011-01-19 16:14:20 -0800 | [diff] [blame] | 14110 | |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14111 | public void run() { |
Mike Cleron | 3ecd58c | 2009-09-28 11:39:02 -0700 | [diff] [blame] | 14112 | long now = AnimationUtils.currentAnimationTimeMillis(); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14113 | if (now >= fadeStartTime) { |
| 14114 | |
| 14115 | // the animation fades the scrollbars out by changing |
| 14116 | // the opacity (alpha) from fully opaque to fully |
| 14117 | // transparent |
| 14118 | int nextFrame = (int) now; |
| 14119 | int framesCount = 0; |
| 14120 | |
| 14121 | Interpolator interpolator = scrollBarInterpolator; |
| 14122 | |
| 14123 | // Start opaque |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 14124 | interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14125 | |
| 14126 | // End transparent |
| 14127 | nextFrame += scrollBarFadeDuration; |
Gilles Debunne | 3dbf55c | 2010-12-16 10:31:51 -0800 | [diff] [blame] | 14128 | interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14129 | |
| 14130 | state = FADING; |
| 14131 | |
| 14132 | // Kick off the fade animation |
Romain Guy | 0fd89bf | 2011-01-26 15:41:30 -0800 | [diff] [blame] | 14133 | host.invalidate(true); |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14134 | } |
| 14135 | } |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 14136 | } |
Mike Cleron | f116bf8 | 2009-09-27 19:14:12 -0700 | [diff] [blame] | 14137 | |
Svetoslav Ganov | a015617 | 2011-06-26 17:55:44 -0700 | [diff] [blame] | 14138 | /** |
| 14139 | * Resuable callback for sending |
| 14140 | * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event. |
| 14141 | */ |
| 14142 | private class SendViewScrolledAccessibilityEvent implements Runnable { |
| 14143 | public volatile boolean mIsPending; |
| 14144 | |
| 14145 | public void run() { |
| 14146 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
| 14147 | mIsPending = false; |
| 14148 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14149 | } |
| 14150 | } |