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 19 import com.android.internal.R; 20 import com.android.internal.view.menu.MenuBuilder; 21 22 import android.content.Context; 23 import android.content.res.Configuration; 24 import android.content.res.Resources; 25 import android.content.res.TypedArray; 26 import android.graphics.Bitmap; 27 import android.graphics.Canvas; 28 import android.graphics.Interpolator; 29 import android.graphics.LinearGradient; 30 import android.graphics.Matrix; 31 import android.graphics.Paint; 32 import android.graphics.PixelFormat; 33 import android.graphics.Point; 34 import android.graphics.PorterDuff; 35 import android.graphics.PorterDuffXfermode; 36 import android.graphics.Rect; 37 import android.graphics.Region; 38 import android.graphics.Shader; 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; 48 import android.os.SystemProperties; 49 import android.util.AttributeSet; 50 import android.util.Config; 51 import android.util.EventLog; 52 import android.util.Log; 53 import android.util.Pool; 54 import android.util.Poolable; 55 import android.util.PoolableManager; 56 import android.util.Pools; 57 import android.util.SparseArray; 58 import android.view.ContextMenu.ContextMenuInfo; 59 import android.view.accessibility.AccessibilityEvent; 60 import android.view.accessibility.AccessibilityEventSource; 61 import android.view.accessibility.AccessibilityManager; 62 import android.view.animation.Animation; 63 import android.view.animation.AnimationUtils; 64 import android.view.inputmethod.EditorInfo; 65 import android.view.inputmethod.InputConnection; 66 import android.view.inputmethod.InputMethodManager; 67 import android.widget.ScrollBarDrawable; 68 69 import java.lang.ref.SoftReference; 70 import java.lang.reflect.InvocationTargetException; 71 import java.lang.reflect.Method; 72 import java.util.ArrayList; 73 import java.util.Arrays; 74 import java.util.WeakHashMap; 75 76 /** 77 * <p> 78 * This class represents the basic building block for user interface components. A View 79 * occupies a rectangular area on the screen and is responsible for drawing and 80 * event handling. View is the base class for <em>widgets</em>, which are 81 * used to create interactive UI components (buttons, text fields, etc.). The 82 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which 83 * are invisible containers that hold other Views (or other ViewGroups) and define 84 * their layout properties. 85 * </p> 86 * 87 * <div class="special"> 88 * <p>For an introduction to using this class to develop your 89 * application's user interface, read the Developer Guide documentation on 90 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics 91 * include: 92 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a> 93 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a> 94 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a> 95 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a> 96 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a> 97 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a> 98 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a> 99 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>. 100 * </p> 101 * </div> 102 * 103 * <a name="Using"></a> 104 * <h3>Using Views</h3> 105 * <p> 106 * All of the views in a window are arranged in a single tree. You can add views 107 * either from code or by specifying a tree of views in one or more XML layout 108 * files. There are many specialized subclasses of views that act as controls or 109 * are capable of displaying text, images, or other content. 110 * </p> 111 * <p> 112 * Once you have created a tree of views, there are typically a few types of 113 * common operations you may wish to perform: 114 * <ul> 115 * <li><strong>Set properties:</strong> for example setting the text of a 116 * {@link android.widget.TextView}. The available properties and the methods 117 * that set them will vary among the different subclasses of views. Note that 118 * properties that are known at build time can be set in the XML layout 119 * files.</li> 120 * <li><strong>Set focus:</strong> The framework will handled moving focus in 121 * response to user input. To force focus to a specific view, call 122 * {@link #requestFocus}.</li> 123 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners 124 * that will be notified when something interesting happens to the view. For 125 * example, all views will let you set a listener to be notified when the view 126 * gains or loses focus. You can register such a listener using 127 * {@link #setOnFocusChangeListener}. Other view subclasses offer more 128 * specialized listeners. For example, a Button exposes a listener to notify 129 * clients when the button is clicked.</li> 130 * <li><strong>Set visibility:</strong> You can hide or show views using 131 * {@link #setVisibility}.</li> 132 * </ul> 133 * </p> 134 * <p><em> 135 * Note: The Android framework is responsible for measuring, laying out and 136 * drawing views. You should not call methods that perform these actions on 137 * views yourself unless you are actually implementing a 138 * {@link android.view.ViewGroup}. 139 * </em></p> 140 * 141 * <a name="Lifecycle"></a> 142 * <h3>Implementing a Custom View</h3> 143 * 144 * <p> 145 * To implement a custom view, you will usually begin by providing overrides for 146 * some of the standard methods that the framework calls on all views. You do 147 * not need to override all of these methods. In fact, you can start by just 148 * overriding {@link #onDraw(android.graphics.Canvas)}. 149 * <table border="2" width="85%" align="center" cellpadding="5"> 150 * <thead> 151 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr> 152 * </thead> 153 * 154 * <tbody> 155 * <tr> 156 * <td rowspan="2">Creation</td> 157 * <td>Constructors</td> 158 * <td>There is a form of the constructor that are called when the view 159 * is created from code and a form that is called when the view is 160 * inflated from a layout file. The second form should parse and apply 161 * any attributes defined in the layout file. 162 * </td> 163 * </tr> 164 * <tr> 165 * <td><code>{@link #onFinishInflate()}</code></td> 166 * <td>Called after a view and all of its children has been inflated 167 * from XML.</td> 168 * </tr> 169 * 170 * <tr> 171 * <td rowspan="3">Layout</td> 172 * <td><code>{@link #onMeasure}</code></td> 173 * <td>Called to determine the size requirements for this view and all 174 * of its children. 175 * </td> 176 * </tr> 177 * <tr> 178 * <td><code>{@link #onLayout}</code></td> 179 * <td>Called when this view should assign a size and position to all 180 * of its children. 181 * </td> 182 * </tr> 183 * <tr> 184 * <td><code>{@link #onSizeChanged}</code></td> 185 * <td>Called when the size of this view has changed. 186 * </td> 187 * </tr> 188 * 189 * <tr> 190 * <td>Drawing</td> 191 * <td><code>{@link #onDraw}</code></td> 192 * <td>Called when the view should render its content. 193 * </td> 194 * </tr> 195 * 196 * <tr> 197 * <td rowspan="4">Event processing</td> 198 * <td><code>{@link #onKeyDown}</code></td> 199 * <td>Called when a new key event occurs. 200 * </td> 201 * </tr> 202 * <tr> 203 * <td><code>{@link #onKeyUp}</code></td> 204 * <td>Called when a key up event occurs. 205 * </td> 206 * </tr> 207 * <tr> 208 * <td><code>{@link #onTrackballEvent}</code></td> 209 * <td>Called when a trackball motion event occurs. 210 * </td> 211 * </tr> 212 * <tr> 213 * <td><code>{@link #onTouchEvent}</code></td> 214 * <td>Called when a touch screen motion event occurs. 215 * </td> 216 * </tr> 217 * 218 * <tr> 219 * <td rowspan="2">Focus</td> 220 * <td><code>{@link #onFocusChanged}</code></td> 221 * <td>Called when the view gains or loses focus. 222 * </td> 223 * </tr> 224 * 225 * <tr> 226 * <td><code>{@link #onWindowFocusChanged}</code></td> 227 * <td>Called when the window containing the view gains or loses focus. 228 * </td> 229 * </tr> 230 * 231 * <tr> 232 * <td rowspan="3">Attaching</td> 233 * <td><code>{@link #onAttachedToWindow()}</code></td> 234 * <td>Called when the view is attached to a window. 235 * </td> 236 * </tr> 237 * 238 * <tr> 239 * <td><code>{@link #onDetachedFromWindow}</code></td> 240 * <td>Called when the view is detached from its window. 241 * </td> 242 * </tr> 243 * 244 * <tr> 245 * <td><code>{@link #onWindowVisibilityChanged}</code></td> 246 * <td>Called when the visibility of the window containing the view 247 * has changed. 248 * </td> 249 * </tr> 250 * </tbody> 251 * 252 * </table> 253 * </p> 254 * 255 * <a name="IDs"></a> 256 * <h3>IDs</h3> 257 * Views may have an integer id associated with them. These ids are typically 258 * assigned in the layout XML files, and are used to find specific views within 259 * the view tree. A common pattern is to: 260 * <ul> 261 * <li>Define a Button in the layout file and assign it a unique ID. 262 * <pre> 263 * <Button id="@+id/my_button" 264 * android:layout_width="wrap_content" 265 * android:layout_height="wrap_content" 266 * android:text="@string/my_button_text"/> 267 * </pre></li> 268 * <li>From the onCreate method of an Activity, find the Button 269 * <pre class="prettyprint"> 270 * Button myButton = (Button) findViewById(R.id.my_button); 271 * </pre></li> 272 * </ul> 273 * <p> 274 * View IDs need not be unique throughout the tree, but it is good practice to 275 * ensure that they are at least unique within the part of the tree you are 276 * searching. 277 * </p> 278 * 279 * <a name="Position"></a> 280 * <h3>Position</h3> 281 * <p> 282 * The geometry of a view is that of a rectangle. A view has a location, 283 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and 284 * two dimensions, expressed as a width and a height. The unit for location 285 * and dimensions is the pixel. 286 * </p> 287 * 288 * <p> 289 * It is possible to retrieve the location of a view by invoking the methods 290 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X, 291 * coordinate of the rectangle representing the view. The latter returns the 292 * top, or Y, coordinate of the rectangle representing the view. These methods 293 * both return the location of the view relative to its parent. For instance, 294 * when getLeft() returns 20, that means the view is located 20 pixels to the 295 * right of the left edge of its direct parent. 296 * </p> 297 * 298 * <p> 299 * In addition, several convenience methods are offered to avoid unnecessary 300 * computations, namely {@link #getRight()} and {@link #getBottom()}. 301 * These methods return the coordinates of the right and bottom edges of the 302 * rectangle representing the view. For instance, calling {@link #getRight()} 303 * is similar to the following computation: <code>getLeft() + getWidth()</code> 304 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.) 305 * </p> 306 * 307 * <a name="SizePaddingMargins"></a> 308 * <h3>Size, padding and margins</h3> 309 * <p> 310 * The size of a view is expressed with a width and a height. A view actually 311 * possess two pairs of width and height values. 312 * </p> 313 * 314 * <p> 315 * The first pair is known as <em>measured width</em> and 316 * <em>measured height</em>. These dimensions define how big a view wants to be 317 * within its parent (see <a href="#Layout">Layout</a> for more details.) The 318 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()} 319 * and {@link #getMeasuredHeight()}. 320 * </p> 321 * 322 * <p> 323 * The second pair is simply known as <em>width</em> and <em>height</em>, or 324 * sometimes <em>drawing width</em> and <em>drawing height</em>. These 325 * dimensions define the actual size of the view on screen, at drawing time and 326 * after layout. These values may, but do not have to, be different from the 327 * measured width and height. The width and height can be obtained by calling 328 * {@link #getWidth()} and {@link #getHeight()}. 329 * </p> 330 * 331 * <p> 332 * To measure its dimensions, a view takes into account its padding. The padding 333 * is expressed in pixels for the left, top, right and bottom parts of the view. 334 * Padding can be used to offset the content of the view by a specific amount of 335 * pixels. For instance, a left padding of 2 will push the view's content by 336 * 2 pixels to the right of the left edge. Padding can be set using the 337 * {@link #setPadding(int, int, int, int)} method and queried by calling 338 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, 339 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}. 340 * </p> 341 * 342 * <p> 343 * Even though a view can define a padding, it does not provide any support for 344 * margins. However, view groups provide such a support. Refer to 345 * {@link android.view.ViewGroup} and 346 * {@link android.view.ViewGroup.MarginLayoutParams} for further information. 347 * </p> 348 * 349 * <a name="Layout"></a> 350 * <h3>Layout</h3> 351 * <p> 352 * Layout is a two pass process: a measure pass and a layout pass. The measuring 353 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal 354 * of the view tree. Each view pushes dimension specifications down the tree 355 * during the recursion. At the end of the measure pass, every view has stored 356 * its measurements. The second pass happens in 357 * {@link #layout(int,int,int,int)} and is also top-down. During 358 * this pass each parent is responsible for positioning all of its children 359 * using the sizes computed in the measure pass. 360 * </p> 361 * 362 * <p> 363 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and 364 * {@link #getMeasuredHeight()} values must be set, along with those for all of 365 * that view's descendants. A view's measured width and measured height values 366 * must respect the constraints imposed by the view's parents. This guarantees 367 * that at the end of the measure pass, all parents accept all of their 368 * children's measurements. A parent view may call measure() more than once on 369 * its children. For example, the parent may measure each child once with 370 * unspecified dimensions to find out how big they want to be, then call 371 * measure() on them again with actual numbers if the sum of all the children's 372 * unconstrained sizes is too big or too small. 373 * </p> 374 * 375 * <p> 376 * The measure pass uses two classes to communicate dimensions. The 377 * {@link MeasureSpec} class is used by views to tell their parents how they 378 * want to be measured and positioned. The base LayoutParams class just 379 * describes how big the view wants to be for both width and height. For each 380 * dimension, it can specify one of: 381 * <ul> 382 * <li> an exact number 383 * <li>MATCH_PARENT, which means the view wants to be as big as its parent 384 * (minus padding) 385 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to 386 * enclose its content (plus padding). 387 * </ul> 388 * There are subclasses of LayoutParams for different subclasses of ViewGroup. 389 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds 390 * an X and Y value. 391 * </p> 392 * 393 * <p> 394 * MeasureSpecs are used to push requirements down the tree from parent to 395 * child. A MeasureSpec can be in one of three modes: 396 * <ul> 397 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension 398 * of a child view. For example, a LinearLayout may call measure() on its child 399 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how 400 * tall the child view wants to be given a width of 240 pixels. 401 * <li>EXACTLY: This is used by the parent to impose an exact size on the 402 * child. The child must use this size, and guarantee that all of its 403 * descendants will fit within this size. 404 * <li>AT_MOST: This is used by the parent to impose a maximum size on the 405 * child. The child must gurantee that it and all of its descendants will fit 406 * within this size. 407 * </ul> 408 * </p> 409 * 410 * <p> 411 * To intiate a layout, call {@link #requestLayout}. This method is typically 412 * called by a view on itself when it believes that is can no longer fit within 413 * its current bounds. 414 * </p> 415 * 416 * <a name="Drawing"></a> 417 * <h3>Drawing</h3> 418 * <p> 419 * Drawing is handled by walking the tree and rendering each view that 420 * intersects the the invalid region. Because the tree is traversed in-order, 421 * this means that parents will draw before (i.e., behind) their children, with 422 * siblings drawn in the order they appear in the tree. 423 * If you set a background drawable for a View, then the View will draw it for you 424 * before calling back to its <code>onDraw()</code> method. 425 * </p> 426 * 427 * <p> 428 * Note that the framework will not draw views that are not in the invalid region. 429 * </p> 430 * 431 * <p> 432 * To force a view to draw, call {@link #invalidate()}. 433 * </p> 434 * 435 * <a name="EventHandlingThreading"></a> 436 * <h3>Event Handling and Threading</h3> 437 * <p> 438 * The basic cycle of a view is as follows: 439 * <ol> 440 * <li>An event comes in and is dispatched to the appropriate view. The view 441 * handles the event and notifies any listeners.</li> 442 * <li>If in the course of processing the event, the view's bounds may need 443 * to be changed, the view will call {@link #requestLayout()}.</li> 444 * <li>Similarly, if in the course of processing the event the view's appearance 445 * may need to be changed, the view will call {@link #invalidate()}.</li> 446 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called, 447 * the framework will take care of measuring, laying out, and drawing the tree 448 * as appropriate.</li> 449 * </ol> 450 * </p> 451 * 452 * <p><em>Note: The entire view tree is single threaded. You must always be on 453 * the UI thread when calling any method on any view.</em> 454 * If you are doing work on other threads and want to update the state of a view 455 * from that thread, you should use a {@link Handler}. 456 * </p> 457 * 458 * <a name="FocusHandling"></a> 459 * <h3>Focus Handling</h3> 460 * <p> 461 * The framework will handle routine focus movement in response to user input. 462 * This includes changing the focus as views are removed or hidden, or as new 463 * views become available. Views indicate their willingness to take focus 464 * through the {@link #isFocusable} method. To change whether a view can take 465 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below) 466 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode} 467 * and can change this via {@link #setFocusableInTouchMode(boolean)}. 468 * </p> 469 * <p> 470 * Focus movement is based on an algorithm which finds the nearest neighbor in a 471 * given direction. In rare cases, the default algorithm may not match the 472 * intended behavior of the developer. In these situations, you can provide 473 * explicit overrides by using these XML attributes in the layout file: 474 * <pre> 475 * nextFocusDown 476 * nextFocusLeft 477 * nextFocusRight 478 * nextFocusUp 479 * </pre> 480 * </p> 481 * 482 * 483 * <p> 484 * To get a particular view to take focus, call {@link #requestFocus()}. 485 * </p> 486 * 487 * <a name="TouchMode"></a> 488 * <h3>Touch Mode</h3> 489 * <p> 490 * When a user is navigating a user interface via directional keys such as a D-pad, it is 491 * necessary to give focus to actionable items such as buttons so the user can see 492 * what will take input. If the device has touch capabilities, however, and the user 493 * begins interacting with the interface by touching it, it is no longer necessary to 494 * always highlight, or give focus to, a particular view. This motivates a mode 495 * for interaction named 'touch mode'. 496 * </p> 497 * <p> 498 * For a touch capable device, once the user touches the screen, the device 499 * will enter touch mode. From this point onward, only views for which 500 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets. 501 * Other views that are touchable, like buttons, will not take focus when touched; they will 502 * only fire the on click listeners. 503 * </p> 504 * <p> 505 * Any time a user hits a directional key, such as a D-pad direction, the view device will 506 * exit touch mode, and find a view to take focus, so that the user may resume interacting 507 * with the user interface without touching the screen again. 508 * </p> 509 * <p> 510 * The touch mode state is maintained across {@link android.app.Activity}s. Call 511 * {@link #isInTouchMode} to see whether the device is currently in touch mode. 512 * </p> 513 * 514 * <a name="Scrolling"></a> 515 * <h3>Scrolling</h3> 516 * <p> 517 * The framework provides basic support for views that wish to internally 518 * scroll their content. This includes keeping track of the X and Y scroll 519 * offset as well as mechanisms for drawing scrollbars. See 520 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and 521 * {@link #awakenScrollBars()} for more details. 522 * </p> 523 * 524 * <a name="Tags"></a> 525 * <h3>Tags</h3> 526 * <p> 527 * Unlike IDs, tags are not used to identify views. Tags are essentially an 528 * extra piece of information that can be associated with a view. They are most 529 * often used as a convenience to store data related to views in the views 530 * themselves rather than by putting them in a separate structure. 531 * </p> 532 * 533 * <a name="Animation"></a> 534 * <h3>Animation</h3> 535 * <p> 536 * You can attach an {@link Animation} object to a view using 537 * {@link #setAnimation(Animation)} or 538 * {@link #startAnimation(Animation)}. The animation can alter the scale, 539 * rotation, translation and alpha of a view over time. If the animation is 540 * attached to a view that has children, the animation will affect the entire 541 * subtree rooted by that node. When an animation is started, the framework will 542 * take care of redrawing the appropriate views until the animation completes. 543 * </p> 544 * 545 * <a name="Security"></a> 546 * <h3>Security</h3> 547 * <p> 548 * Sometimes it is essential that an application be able to verify that an action 549 * is being performed with the full knowledge and consent of the user, such as 550 * granting a permission request, making a purchase or clicking on an advertisement. 551 * Unfortunately, a malicious application could try to spoof the user into 552 * performing these actions, unaware, by concealing the intended purpose of the view. 553 * As a remedy, the framework offers a touch filtering mechanism that can be used to 554 * improve the security of views that provide access to sensitive functionality. 555 * </p><p> 556 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the 557 * andoird:filterTouchesWhenObscured attribute to true. When enabled, the framework 558 * will discard touches that are received whenever the view's window is obscured by 559 * another visible window. As a result, the view will not receive touches whenever a 560 * toast, dialog or other window appears above the view's window. 561 * </p><p> 562 * For more fine-grained control over security, consider overriding the 563 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy. 564 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}. 565 * </p> 566 * 567 * @attr ref android.R.styleable#View_background 568 * @attr ref android.R.styleable#View_clickable 569 * @attr ref android.R.styleable#View_contentDescription 570 * @attr ref android.R.styleable#View_drawingCacheQuality 571 * @attr ref android.R.styleable#View_duplicateParentState 572 * @attr ref android.R.styleable#View_id 573 * @attr ref android.R.styleable#View_fadingEdge 574 * @attr ref android.R.styleable#View_fadingEdgeLength 575 * @attr ref android.R.styleable#View_filterTouchesWhenObscured 576 * @attr ref android.R.styleable#View_fitsSystemWindows 577 * @attr ref android.R.styleable#View_isScrollContainer 578 * @attr ref android.R.styleable#View_focusable 579 * @attr ref android.R.styleable#View_focusableInTouchMode 580 * @attr ref android.R.styleable#View_hapticFeedbackEnabled 581 * @attr ref android.R.styleable#View_keepScreenOn 582 * @attr ref android.R.styleable#View_longClickable 583 * @attr ref android.R.styleable#View_minHeight 584 * @attr ref android.R.styleable#View_minWidth 585 * @attr ref android.R.styleable#View_nextFocusDown 586 * @attr ref android.R.styleable#View_nextFocusLeft 587 * @attr ref android.R.styleable#View_nextFocusRight 588 * @attr ref android.R.styleable#View_nextFocusUp 589 * @attr ref android.R.styleable#View_onClick 590 * @attr ref android.R.styleable#View_padding 591 * @attr ref android.R.styleable#View_paddingBottom 592 * @attr ref android.R.styleable#View_paddingLeft 593 * @attr ref android.R.styleable#View_paddingRight 594 * @attr ref android.R.styleable#View_paddingTop 595 * @attr ref android.R.styleable#View_saveEnabled 596 * @attr ref android.R.styleable#View_scrollX 597 * @attr ref android.R.styleable#View_scrollY 598 * @attr ref android.R.styleable#View_scrollbarSize 599 * @attr ref android.R.styleable#View_scrollbarStyle 600 * @attr ref android.R.styleable#View_scrollbars 601 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade 602 * @attr ref android.R.styleable#View_scrollbarFadeDuration 603 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal 604 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal 605 * @attr ref android.R.styleable#View_scrollbarThumbVertical 606 * @attr ref android.R.styleable#View_scrollbarTrackVertical 607 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack 608 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack 609 * @attr ref android.R.styleable#View_soundEffectsEnabled 610 * @attr ref android.R.styleable#View_tag 611 * @attr ref android.R.styleable#View_visibility 612 * 613 * @see android.view.ViewGroup 614 */ 615 public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { 616 private static final boolean DBG = false; 617 618 /** 619 * The logging tag used by this class with android.util.Log. 620 */ 621 protected static final String VIEW_LOG_TAG = "View"; 622 623 /** 624 * Used to mark a View that has no ID. 625 */ 626 public static final int NO_ID = -1; 627 628 /** 629 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when 630 * calling setFlags. 631 */ 632 private static final int NOT_FOCUSABLE = 0x00000000; 633 634 /** 635 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling 636 * setFlags. 637 */ 638 private static final int FOCUSABLE = 0x00000001; 639 640 /** 641 * Mask for use with setFlags indicating bits used for focus. 642 */ 643 private static final int FOCUSABLE_MASK = 0x00000001; 644 645 /** 646 * This view will adjust its padding to fit sytem windows (e.g. status bar) 647 */ 648 private static final int FITS_SYSTEM_WINDOWS = 0x00000002; 649 650 /** 651 * This view is visible. Use with {@link #setVisibility}. 652 */ 653 public static final int VISIBLE = 0x00000000; 654 655 /** 656 * This view is invisible, but it still takes up space for layout purposes. 657 * Use with {@link #setVisibility}. 658 */ 659 public static final int INVISIBLE = 0x00000004; 660 661 /** 662 * This view is invisible, and it doesn't take any space for layout 663 * purposes. Use with {@link #setVisibility}. 664 */ 665 public static final int GONE = 0x00000008; 666 667 /** 668 * Mask for use with setFlags indicating bits used for visibility. 669 * {@hide} 670 */ 671 static final int VISIBILITY_MASK = 0x0000000C; 672 673 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE}; 674 675 /** 676 * This view is enabled. Intrepretation varies by subclass. 677 * Use with ENABLED_MASK when calling setFlags. 678 * {@hide} 679 */ 680 static final int ENABLED = 0x00000000; 681 682 /** 683 * This view is disabled. Intrepretation varies by subclass. 684 * Use with ENABLED_MASK when calling setFlags. 685 * {@hide} 686 */ 687 static final int DISABLED = 0x00000020; 688 689 /** 690 * Mask for use with setFlags indicating bits used for indicating whether 691 * this view is enabled 692 * {@hide} 693 */ 694 static final int ENABLED_MASK = 0x00000020; 695 696 /** 697 * This view won't draw. {@link #onDraw} won't be called and further 698 * optimizations 699 * will be performed. It is okay to have this flag set and a background. 700 * Use with DRAW_MASK when calling setFlags. 701 * {@hide} 702 */ 703 static final int WILL_NOT_DRAW = 0x00000080; 704 705 /** 706 * Mask for use with setFlags indicating bits used for indicating whether 707 * this view is will draw 708 * {@hide} 709 */ 710 static final int DRAW_MASK = 0x00000080; 711 712 /** 713 * <p>This view doesn't show scrollbars.</p> 714 * {@hide} 715 */ 716 static final int SCROLLBARS_NONE = 0x00000000; 717 718 /** 719 * <p>This view shows horizontal scrollbars.</p> 720 * {@hide} 721 */ 722 static final int SCROLLBARS_HORIZONTAL = 0x00000100; 723 724 /** 725 * <p>This view shows vertical scrollbars.</p> 726 * {@hide} 727 */ 728 static final int SCROLLBARS_VERTICAL = 0x00000200; 729 730 /** 731 * <p>Mask for use with setFlags indicating bits used for indicating which 732 * scrollbars are enabled.</p> 733 * {@hide} 734 */ 735 static final int SCROLLBARS_MASK = 0x00000300; 736 737 /** 738 * Indicates that the view should filter touches when its window is obscured. 739 * Refer to the class comments for more information about this security feature. 740 * {@hide} 741 */ 742 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400; 743 744 // note flag value 0x00000800 is now available for next flags... 745 746 /** 747 * <p>This view doesn't show fading edges.</p> 748 * {@hide} 749 */ 750 static final int FADING_EDGE_NONE = 0x00000000; 751 752 /** 753 * <p>This view shows horizontal fading edges.</p> 754 * {@hide} 755 */ 756 static final int FADING_EDGE_HORIZONTAL = 0x00001000; 757 758 /** 759 * <p>This view shows vertical fading edges.</p> 760 * {@hide} 761 */ 762 static final int FADING_EDGE_VERTICAL = 0x00002000; 763 764 /** 765 * <p>Mask for use with setFlags indicating bits used for indicating which 766 * fading edges are enabled.</p> 767 * {@hide} 768 */ 769 static final int FADING_EDGE_MASK = 0x00003000; 770 771 /** 772 * <p>Indicates this view can be clicked. When clickable, a View reacts 773 * to clicks by notifying the OnClickListener.<p> 774 * {@hide} 775 */ 776 static final int CLICKABLE = 0x00004000; 777 778 /** 779 * <p>Indicates this view is caching its drawing into a bitmap.</p> 780 * {@hide} 781 */ 782 static final int DRAWING_CACHE_ENABLED = 0x00008000; 783 784 /** 785 * <p>Indicates that no icicle should be saved for this view.<p> 786 * {@hide} 787 */ 788 static final int SAVE_DISABLED = 0x000010000; 789 790 /** 791 * <p>Mask for use with setFlags indicating bits used for the saveEnabled 792 * property.</p> 793 * {@hide} 794 */ 795 static final int SAVE_DISABLED_MASK = 0x000010000; 796 797 /** 798 * <p>Indicates that no drawing cache should ever be created for this view.<p> 799 * {@hide} 800 */ 801 static final int WILL_NOT_CACHE_DRAWING = 0x000020000; 802 803 /** 804 * <p>Indicates this view can take / keep focus when int touch mode.</p> 805 * {@hide} 806 */ 807 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000; 808 809 /** 810 * <p>Enables low quality mode for the drawing cache.</p> 811 */ 812 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000; 813 814 /** 815 * <p>Enables high quality mode for the drawing cache.</p> 816 */ 817 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000; 818 819 /** 820 * <p>Enables automatic quality mode for the drawing cache.</p> 821 */ 822 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000; 823 824 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = { 825 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH 826 }; 827 828 /** 829 * <p>Mask for use with setFlags indicating bits used for the cache 830 * quality property.</p> 831 * {@hide} 832 */ 833 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000; 834 835 /** 836 * <p> 837 * Indicates this view can be long clicked. When long clickable, a View 838 * reacts to long clicks by notifying the OnLongClickListener or showing a 839 * context menu. 840 * </p> 841 * {@hide} 842 */ 843 static final int LONG_CLICKABLE = 0x00200000; 844 845 /** 846 * <p>Indicates that this view gets its drawable states from its direct parent 847 * and ignores its original internal states.</p> 848 * 849 * @hide 850 */ 851 static final int DUPLICATE_PARENT_STATE = 0x00400000; 852 853 /** 854 * The scrollbar style to display the scrollbars inside the content area, 855 * without increasing the padding. The scrollbars will be overlaid with 856 * translucency on the view's content. 857 */ 858 public static final int SCROLLBARS_INSIDE_OVERLAY = 0; 859 860 /** 861 * The scrollbar style to display the scrollbars inside the padded area, 862 * increasing the padding of the view. The scrollbars will not overlap the 863 * content area of the view. 864 */ 865 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000; 866 867 /** 868 * The scrollbar style to display the scrollbars at the edge of the view, 869 * without increasing the padding. The scrollbars will be overlaid with 870 * translucency. 871 */ 872 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000; 873 874 /** 875 * The scrollbar style to display the scrollbars at the edge of the view, 876 * increasing the padding of the view. The scrollbars will only overlap the 877 * background, if any. 878 */ 879 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000; 880 881 /** 882 * Mask to check if the scrollbar style is overlay or inset. 883 * {@hide} 884 */ 885 static final int SCROLLBARS_INSET_MASK = 0x01000000; 886 887 /** 888 * Mask to check if the scrollbar style is inside or outside. 889 * {@hide} 890 */ 891 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000; 892 893 /** 894 * Mask for scrollbar style. 895 * {@hide} 896 */ 897 static final int SCROLLBARS_STYLE_MASK = 0x03000000; 898 899 /** 900 * View flag indicating that the screen should remain on while the 901 * window containing this view is visible to the user. This effectively 902 * takes care of automatically setting the WindowManager's 903 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}. 904 */ 905 public static final int KEEP_SCREEN_ON = 0x04000000; 906 907 /** 908 * View flag indicating whether this view should have sound effects enabled 909 * for events such as clicking and touching. 910 */ 911 public static final int SOUND_EFFECTS_ENABLED = 0x08000000; 912 913 /** 914 * View flag indicating whether this view should have haptic feedback 915 * enabled for events such as long presses. 916 */ 917 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000; 918 919 /** 920 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)} 921 * should add all focusable Views regardless if they are focusable in touch mode. 922 */ 923 public static final int FOCUSABLES_ALL = 0x00000000; 924 925 /** 926 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)} 927 * should add only Views focusable in touch mode. 928 */ 929 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001; 930 931 /** 932 * Use with {@link #focusSearch}. Move focus to the previous selectable 933 * item. 934 */ 935 public static final int FOCUS_BACKWARD = 0x00000001; 936 937 /** 938 * Use with {@link #focusSearch}. Move focus to the next selectable 939 * item. 940 */ 941 public static final int FOCUS_FORWARD = 0x00000002; 942 943 /** 944 * Use with {@link #focusSearch}. Move focus to the left. 945 */ 946 public static final int FOCUS_LEFT = 0x00000011; 947 948 /** 949 * Use with {@link #focusSearch}. Move focus up. 950 */ 951 public static final int FOCUS_UP = 0x00000021; 952 953 /** 954 * Use with {@link #focusSearch}. Move focus to the right. 955 */ 956 public static final int FOCUS_RIGHT = 0x00000042; 957 958 /** 959 * Use with {@link #focusSearch}. Move focus down. 960 */ 961 public static final int FOCUS_DOWN = 0x00000082; 962 963 /** 964 * Base View state sets 965 */ 966 // Singles 967 /** 968 * Indicates the view has no states set. States are used with 969 * {@link android.graphics.drawable.Drawable} to change the drawing of the 970 * view depending on its state. 971 * 972 * @see android.graphics.drawable.Drawable 973 * @see #getDrawableState() 974 */ 975 protected static final int[] EMPTY_STATE_SET = {}; 976 /** 977 * Indicates the view is enabled. States are used with 978 * {@link android.graphics.drawable.Drawable} to change the drawing of the 979 * view depending on its state. 980 * 981 * @see android.graphics.drawable.Drawable 982 * @see #getDrawableState() 983 */ 984 protected static final int[] ENABLED_STATE_SET = {R.attr.state_enabled}; 985 /** 986 * Indicates the view is focused. States are used with 987 * {@link android.graphics.drawable.Drawable} to change the drawing of the 988 * view depending on its state. 989 * 990 * @see android.graphics.drawable.Drawable 991 * @see #getDrawableState() 992 */ 993 protected static final int[] FOCUSED_STATE_SET = {R.attr.state_focused}; 994 /** 995 * Indicates the view is selected. States are used with 996 * {@link android.graphics.drawable.Drawable} to change the drawing of the 997 * view depending on its state. 998 * 999 * @see android.graphics.drawable.Drawable 1000 * @see #getDrawableState() 1001 */ 1002 protected static final int[] SELECTED_STATE_SET = {R.attr.state_selected}; 1003 /** 1004 * Indicates the view is pressed. States are used with 1005 * {@link android.graphics.drawable.Drawable} to change the drawing of the 1006 * view depending on its state. 1007 * 1008 * @see android.graphics.drawable.Drawable 1009 * @see #getDrawableState() 1010 * @hide 1011 */ 1012 protected static final int[] PRESSED_STATE_SET = {R.attr.state_pressed}; 1013 /** 1014 * Indicates the view's window has focus. States are used with 1015 * {@link android.graphics.drawable.Drawable} to change the drawing of the 1016 * view depending on its state. 1017 * 1018 * @see android.graphics.drawable.Drawable 1019 * @see #getDrawableState() 1020 */ 1021 protected static final int[] WINDOW_FOCUSED_STATE_SET = 1022 {R.attr.state_window_focused}; 1023 // Doubles 1024 /** 1025 * Indicates the view is enabled and has the focus. 1026 * 1027 * @see #ENABLED_STATE_SET 1028 * @see #FOCUSED_STATE_SET 1029 */ 1030 protected static final int[] ENABLED_FOCUSED_STATE_SET = 1031 stateSetUnion(ENABLED_STATE_SET, FOCUSED_STATE_SET); 1032 /** 1033 * Indicates the view is enabled and selected. 1034 * 1035 * @see #ENABLED_STATE_SET 1036 * @see #SELECTED_STATE_SET 1037 */ 1038 protected static final int[] ENABLED_SELECTED_STATE_SET = 1039 stateSetUnion(ENABLED_STATE_SET, SELECTED_STATE_SET); 1040 /** 1041 * Indicates the view is enabled and that its window has focus. 1042 * 1043 * @see #ENABLED_STATE_SET 1044 * @see #WINDOW_FOCUSED_STATE_SET 1045 */ 1046 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET = 1047 stateSetUnion(ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1048 /** 1049 * Indicates the view is focused and selected. 1050 * 1051 * @see #FOCUSED_STATE_SET 1052 * @see #SELECTED_STATE_SET 1053 */ 1054 protected static final int[] FOCUSED_SELECTED_STATE_SET = 1055 stateSetUnion(FOCUSED_STATE_SET, SELECTED_STATE_SET); 1056 /** 1057 * Indicates the view has the focus and that its window has the focus. 1058 * 1059 * @see #FOCUSED_STATE_SET 1060 * @see #WINDOW_FOCUSED_STATE_SET 1061 */ 1062 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET = 1063 stateSetUnion(FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1064 /** 1065 * Indicates the view is selected and that its window has the focus. 1066 * 1067 * @see #SELECTED_STATE_SET 1068 * @see #WINDOW_FOCUSED_STATE_SET 1069 */ 1070 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET = 1071 stateSetUnion(SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1072 // Triples 1073 /** 1074 * Indicates the view is enabled, focused and selected. 1075 * 1076 * @see #ENABLED_STATE_SET 1077 * @see #FOCUSED_STATE_SET 1078 * @see #SELECTED_STATE_SET 1079 */ 1080 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET = 1081 stateSetUnion(ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET); 1082 /** 1083 * Indicates the view is enabled, focused and its window has the focus. 1084 * 1085 * @see #ENABLED_STATE_SET 1086 * @see #FOCUSED_STATE_SET 1087 * @see #WINDOW_FOCUSED_STATE_SET 1088 */ 1089 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = 1090 stateSetUnion(ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1091 /** 1092 * Indicates the view is enabled, selected and its window has the focus. 1093 * 1094 * @see #ENABLED_STATE_SET 1095 * @see #SELECTED_STATE_SET 1096 * @see #WINDOW_FOCUSED_STATE_SET 1097 */ 1098 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1099 stateSetUnion(ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1100 /** 1101 * Indicates the view is focused, selected and its window has the focus. 1102 * 1103 * @see #FOCUSED_STATE_SET 1104 * @see #SELECTED_STATE_SET 1105 * @see #WINDOW_FOCUSED_STATE_SET 1106 */ 1107 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1108 stateSetUnion(FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1109 /** 1110 * Indicates the view is enabled, focused, selected and its window 1111 * has the focus. 1112 * 1113 * @see #ENABLED_STATE_SET 1114 * @see #FOCUSED_STATE_SET 1115 * @see #SELECTED_STATE_SET 1116 * @see #WINDOW_FOCUSED_STATE_SET 1117 */ 1118 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1119 stateSetUnion(ENABLED_FOCUSED_SELECTED_STATE_SET, 1120 WINDOW_FOCUSED_STATE_SET); 1121 1122 /** 1123 * Indicates the view is pressed and its window has the focus. 1124 * 1125 * @see #PRESSED_STATE_SET 1126 * @see #WINDOW_FOCUSED_STATE_SET 1127 */ 1128 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET = 1129 stateSetUnion(PRESSED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1130 1131 /** 1132 * Indicates the view is pressed and selected. 1133 * 1134 * @see #PRESSED_STATE_SET 1135 * @see #SELECTED_STATE_SET 1136 */ 1137 protected static final int[] PRESSED_SELECTED_STATE_SET = 1138 stateSetUnion(PRESSED_STATE_SET, SELECTED_STATE_SET); 1139 1140 /** 1141 * Indicates the view is pressed, selected and its window has the focus. 1142 * 1143 * @see #PRESSED_STATE_SET 1144 * @see #SELECTED_STATE_SET 1145 * @see #WINDOW_FOCUSED_STATE_SET 1146 */ 1147 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1148 stateSetUnion(PRESSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1149 1150 /** 1151 * Indicates the view is pressed and focused. 1152 * 1153 * @see #PRESSED_STATE_SET 1154 * @see #FOCUSED_STATE_SET 1155 */ 1156 protected static final int[] PRESSED_FOCUSED_STATE_SET = 1157 stateSetUnion(PRESSED_STATE_SET, FOCUSED_STATE_SET); 1158 1159 /** 1160 * Indicates the view is pressed, focused and its window has the focus. 1161 * 1162 * @see #PRESSED_STATE_SET 1163 * @see #FOCUSED_STATE_SET 1164 * @see #WINDOW_FOCUSED_STATE_SET 1165 */ 1166 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = 1167 stateSetUnion(PRESSED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1168 1169 /** 1170 * Indicates the view is pressed, focused and selected. 1171 * 1172 * @see #PRESSED_STATE_SET 1173 * @see #SELECTED_STATE_SET 1174 * @see #FOCUSED_STATE_SET 1175 */ 1176 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET = 1177 stateSetUnion(PRESSED_FOCUSED_STATE_SET, SELECTED_STATE_SET); 1178 1179 /** 1180 * Indicates the view is pressed, focused, selected and its window has the focus. 1181 * 1182 * @see #PRESSED_STATE_SET 1183 * @see #FOCUSED_STATE_SET 1184 * @see #SELECTED_STATE_SET 1185 * @see #WINDOW_FOCUSED_STATE_SET 1186 */ 1187 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1188 stateSetUnion(PRESSED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1189 1190 /** 1191 * Indicates the view is pressed and enabled. 1192 * 1193 * @see #PRESSED_STATE_SET 1194 * @see #ENABLED_STATE_SET 1195 */ 1196 protected static final int[] PRESSED_ENABLED_STATE_SET = 1197 stateSetUnion(PRESSED_STATE_SET, ENABLED_STATE_SET); 1198 1199 /** 1200 * Indicates the view is pressed, enabled and its window has the focus. 1201 * 1202 * @see #PRESSED_STATE_SET 1203 * @see #ENABLED_STATE_SET 1204 * @see #WINDOW_FOCUSED_STATE_SET 1205 */ 1206 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = 1207 stateSetUnion(PRESSED_ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1208 1209 /** 1210 * Indicates the view is pressed, enabled and selected. 1211 * 1212 * @see #PRESSED_STATE_SET 1213 * @see #ENABLED_STATE_SET 1214 * @see #SELECTED_STATE_SET 1215 */ 1216 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET = 1217 stateSetUnion(PRESSED_ENABLED_STATE_SET, SELECTED_STATE_SET); 1218 1219 /** 1220 * Indicates the view is pressed, enabled, selected and its window has the 1221 * focus. 1222 * 1223 * @see #PRESSED_STATE_SET 1224 * @see #ENABLED_STATE_SET 1225 * @see #SELECTED_STATE_SET 1226 * @see #WINDOW_FOCUSED_STATE_SET 1227 */ 1228 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1229 stateSetUnion(PRESSED_ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1230 1231 /** 1232 * Indicates the view is pressed, enabled and focused. 1233 * 1234 * @see #PRESSED_STATE_SET 1235 * @see #ENABLED_STATE_SET 1236 * @see #FOCUSED_STATE_SET 1237 */ 1238 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET = 1239 stateSetUnion(PRESSED_ENABLED_STATE_SET, FOCUSED_STATE_SET); 1240 1241 /** 1242 * Indicates the view is pressed, enabled, focused and its window has the 1243 * focus. 1244 * 1245 * @see #PRESSED_STATE_SET 1246 * @see #ENABLED_STATE_SET 1247 * @see #FOCUSED_STATE_SET 1248 * @see #WINDOW_FOCUSED_STATE_SET 1249 */ 1250 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = 1251 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1252 1253 /** 1254 * Indicates the view is pressed, enabled, focused and selected. 1255 * 1256 * @see #PRESSED_STATE_SET 1257 * @see #ENABLED_STATE_SET 1258 * @see #SELECTED_STATE_SET 1259 * @see #FOCUSED_STATE_SET 1260 */ 1261 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = 1262 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET); 1263 1264 /** 1265 * Indicates the view is pressed, enabled, focused, selected and its window 1266 * has the focus. 1267 * 1268 * @see #PRESSED_STATE_SET 1269 * @see #ENABLED_STATE_SET 1270 * @see #SELECTED_STATE_SET 1271 * @see #FOCUSED_STATE_SET 1272 * @see #WINDOW_FOCUSED_STATE_SET 1273 */ 1274 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = 1275 stateSetUnion(PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET); 1276 1277 /** 1278 * The order here is very important to {@link #getDrawableState()} 1279 */ 1280 private static final int[][] VIEW_STATE_SETS = { 1281 EMPTY_STATE_SET, // 0 0 0 0 0 1282 WINDOW_FOCUSED_STATE_SET, // 0 0 0 0 1 1283 SELECTED_STATE_SET, // 0 0 0 1 0 1284 SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 0 1 1 1285 FOCUSED_STATE_SET, // 0 0 1 0 0 1286 FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 0 1 1287 FOCUSED_SELECTED_STATE_SET, // 0 0 1 1 0 1288 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 1 1 1289 ENABLED_STATE_SET, // 0 1 0 0 0 1290 ENABLED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 0 1 1291 ENABLED_SELECTED_STATE_SET, // 0 1 0 1 0 1292 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 1 1 1293 ENABLED_FOCUSED_STATE_SET, // 0 1 1 0 0 1294 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 0 1 1295 ENABLED_FOCUSED_SELECTED_STATE_SET, // 0 1 1 1 0 1296 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 1 1 1297 PRESSED_STATE_SET, // 1 0 0 0 0 1298 PRESSED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 0 1 1299 PRESSED_SELECTED_STATE_SET, // 1 0 0 1 0 1300 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 1 1 1301 PRESSED_FOCUSED_STATE_SET, // 1 0 1 0 0 1302 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 0 1 1303 PRESSED_FOCUSED_SELECTED_STATE_SET, // 1 0 1 1 0 1304 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 1 1 1305 PRESSED_ENABLED_STATE_SET, // 1 1 0 0 0 1306 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 0 1 1307 PRESSED_ENABLED_SELECTED_STATE_SET, // 1 1 0 1 0 1308 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 1 1 1309 PRESSED_ENABLED_FOCUSED_STATE_SET, // 1 1 1 0 0 1310 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 0 1 1311 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, // 1 1 1 1 0 1312 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 1 1 1313 }; 1314 1315 /** 1316 * Used by views that contain lists of items. This state indicates that 1317 * the view is showing the last item. 1318 * @hide 1319 */ 1320 protected static final int[] LAST_STATE_SET = {R.attr.state_last}; 1321 /** 1322 * Used by views that contain lists of items. This state indicates that 1323 * the view is showing the first item. 1324 * @hide 1325 */ 1326 protected static final int[] FIRST_STATE_SET = {R.attr.state_first}; 1327 /** 1328 * Used by views that contain lists of items. This state indicates that 1329 * the view is showing the middle item. 1330 * @hide 1331 */ 1332 protected static final int[] MIDDLE_STATE_SET = {R.attr.state_middle}; 1333 /** 1334 * Used by views that contain lists of items. This state indicates that 1335 * the view is showing only one item. 1336 * @hide 1337 */ 1338 protected static final int[] SINGLE_STATE_SET = {R.attr.state_single}; 1339 /** 1340 * Used by views that contain lists of items. This state indicates that 1341 * the view is pressed and showing the last item. 1342 * @hide 1343 */ 1344 protected static final int[] PRESSED_LAST_STATE_SET = {R.attr.state_last, R.attr.state_pressed}; 1345 /** 1346 * Used by views that contain lists of items. This state indicates that 1347 * the view is pressed and showing the first item. 1348 * @hide 1349 */ 1350 protected static final int[] PRESSED_FIRST_STATE_SET = {R.attr.state_first, R.attr.state_pressed}; 1351 /** 1352 * Used by views that contain lists of items. This state indicates that 1353 * the view is pressed and showing the middle item. 1354 * @hide 1355 */ 1356 protected static final int[] PRESSED_MIDDLE_STATE_SET = {R.attr.state_middle, R.attr.state_pressed}; 1357 /** 1358 * Used by views that contain lists of items. This state indicates that 1359 * the view is pressed and showing only one item. 1360 * @hide 1361 */ 1362 protected static final int[] PRESSED_SINGLE_STATE_SET = {R.attr.state_single, R.attr.state_pressed}; 1363 1364 /** 1365 * Temporary Rect currently for use in setBackground(). This will probably 1366 * be extended in the future to hold our own class with more than just 1367 * a Rect. :) 1368 */ 1369 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>(); 1370 1371 /** 1372 * Map used to store views' tags. 1373 */ 1374 private static WeakHashMap<View, SparseArray<Object>> sTags; 1375 1376 /** 1377 * Lock used to access sTags. 1378 */ 1379 private static final Object sTagsLock = new Object(); 1380 1381 /** 1382 * The animation currently associated with this view. 1383 * @hide 1384 */ 1385 protected Animation mCurrentAnimation = null; 1386 1387 /** 1388 * Width as measured during measure pass. 1389 * {@hide} 1390 */ 1391 @ViewDebug.ExportedProperty(category = "measurement") 1392 protected int mMeasuredWidth; 1393 1394 /** 1395 * Height as measured during measure pass. 1396 * {@hide} 1397 */ 1398 @ViewDebug.ExportedProperty(category = "measurement") 1399 protected int mMeasuredHeight; 1400 1401 /** 1402 * The view's identifier. 1403 * {@hide} 1404 * 1405 * @see #setId(int) 1406 * @see #getId() 1407 */ 1408 @ViewDebug.ExportedProperty(resolveId = true) 1409 int mID = NO_ID; 1410 1411 /** 1412 * The view's tag. 1413 * {@hide} 1414 * 1415 * @see #setTag(Object) 1416 * @see #getTag() 1417 */ 1418 protected Object mTag; 1419 1420 // for mPrivateFlags: 1421 /** {@hide} */ 1422 static final int WANTS_FOCUS = 0x00000001; 1423 /** {@hide} */ 1424 static final int FOCUSED = 0x00000002; 1425 /** {@hide} */ 1426 static final int SELECTED = 0x00000004; 1427 /** {@hide} */ 1428 static final int IS_ROOT_NAMESPACE = 0x00000008; 1429 /** {@hide} */ 1430 static final int HAS_BOUNDS = 0x00000010; 1431 /** {@hide} */ 1432 static final int DRAWN = 0x00000020; 1433 /** 1434 * When this flag is set, this view is running an animation on behalf of its 1435 * children and should therefore not cancel invalidate requests, even if they 1436 * lie outside of this view's bounds. 1437 * 1438 * {@hide} 1439 */ 1440 static final int DRAW_ANIMATION = 0x00000040; 1441 /** {@hide} */ 1442 static final int SKIP_DRAW = 0x00000080; 1443 /** {@hide} */ 1444 static final int ONLY_DRAWS_BACKGROUND = 0x00000100; 1445 /** {@hide} */ 1446 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200; 1447 /** {@hide} */ 1448 static final int DRAWABLE_STATE_DIRTY = 0x00000400; 1449 /** {@hide} */ 1450 static final int MEASURED_DIMENSION_SET = 0x00000800; 1451 /** {@hide} */ 1452 static final int FORCE_LAYOUT = 0x00001000; 1453 /** {@hide} */ 1454 static final int LAYOUT_REQUIRED = 0x00002000; 1455 1456 private static final int PRESSED = 0x00004000; 1457 1458 /** {@hide} */ 1459 static final int DRAWING_CACHE_VALID = 0x00008000; 1460 /** 1461 * Flag used to indicate that this view should be drawn once more (and only once 1462 * more) after its animation has completed. 1463 * {@hide} 1464 */ 1465 static final int ANIMATION_STARTED = 0x00010000; 1466 1467 private static final int SAVE_STATE_CALLED = 0x00020000; 1468 1469 /** 1470 * Indicates that the View returned true when onSetAlpha() was called and that 1471 * the alpha must be restored. 1472 * {@hide} 1473 */ 1474 static final int ALPHA_SET = 0x00040000; 1475 1476 /** 1477 * Set by {@link #setScrollContainer(boolean)}. 1478 */ 1479 static final int SCROLL_CONTAINER = 0x00080000; 1480 1481 /** 1482 * Set by {@link #setScrollContainer(boolean)}. 1483 */ 1484 static final int SCROLL_CONTAINER_ADDED = 0x00100000; 1485 1486 /** 1487 * View flag indicating whether this view was invalidated (fully or partially.) 1488 * 1489 * @hide 1490 */ 1491 static final int DIRTY = 0x00200000; 1492 1493 /** 1494 * View flag indicating whether this view was invalidated by an opaque 1495 * invalidate request. 1496 * 1497 * @hide 1498 */ 1499 static final int DIRTY_OPAQUE = 0x00400000; 1500 1501 /** 1502 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}. 1503 * 1504 * @hide 1505 */ 1506 static final int DIRTY_MASK = 0x00600000; 1507 1508 /** 1509 * Indicates whether the background is opaque. 1510 * 1511 * @hide 1512 */ 1513 static final int OPAQUE_BACKGROUND = 0x00800000; 1514 1515 /** 1516 * Indicates whether the scrollbars are opaque. 1517 * 1518 * @hide 1519 */ 1520 static final int OPAQUE_SCROLLBARS = 0x01000000; 1521 1522 /** 1523 * Indicates whether the view is opaque. 1524 * 1525 * @hide 1526 */ 1527 static final int OPAQUE_MASK = 0x01800000; 1528 1529 /** 1530 * Indicates a prepressed state; 1531 * the short time between ACTION_DOWN and recognizing 1532 * a 'real' press. Prepressed is used to recognize quick taps 1533 * even when they are shorter than ViewConfiguration.getTapTimeout(). 1534 * 1535 * @hide 1536 */ 1537 private static final int PREPRESSED = 0x02000000; 1538 1539 /** 1540 * Indicates whether the view is temporarily detached. 1541 * 1542 * @hide 1543 */ 1544 static final int CANCEL_NEXT_UP_EVENT = 0x04000000; 1545 1546 /** 1547 * Indicates that we should awaken scroll bars once attached 1548 * 1549 * @hide 1550 */ 1551 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000; 1552 1553 /** 1554 * Always allow a user to over-scroll this view, provided it is a 1555 * view that can scroll. 1556 * 1557 * @see #getOverScrollMode() 1558 * @see #setOverScrollMode(int) 1559 */ 1560 public static final int OVER_SCROLL_ALWAYS = 0; 1561 1562 /** 1563 * Allow a user to over-scroll this view only if the content is large 1564 * enough to meaningfully scroll, provided it is a view that can scroll. 1565 * 1566 * @see #getOverScrollMode() 1567 * @see #setOverScrollMode(int) 1568 */ 1569 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1; 1570 1571 /** 1572 * Never allow a user to over-scroll this view. 1573 * 1574 * @see #getOverScrollMode() 1575 * @see #setOverScrollMode(int) 1576 */ 1577 public static final int OVER_SCROLL_NEVER = 2; 1578 1579 /** 1580 * Controls the over-scroll mode for this view. 1581 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)}, 1582 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}, 1583 * and {@link #OVER_SCROLL_NEVER}. 1584 */ 1585 private int mOverScrollMode; 1586 1587 /** 1588 * The parent this view is attached to. 1589 * {@hide} 1590 * 1591 * @see #getParent() 1592 */ 1593 protected ViewParent mParent; 1594 1595 /** 1596 * {@hide} 1597 */ 1598 AttachInfo mAttachInfo; 1599 1600 /** 1601 * {@hide} 1602 */ 1603 @ViewDebug.ExportedProperty(flagMapping = { 1604 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT, 1605 name = "FORCE_LAYOUT"), 1606 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED, 1607 name = "LAYOUT_REQUIRED"), 1608 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID, 1609 name = "DRAWING_CACHE_INVALID", outputIf = false), 1610 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true), 1611 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false), 1612 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"), 1613 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY") 1614 }) 1615 int mPrivateFlags; 1616 1617 /** 1618 * Count of how many windows this view has been attached to. 1619 */ 1620 int mWindowAttachCount; 1621 1622 /** 1623 * The layout parameters associated with this view and used by the parent 1624 * {@link android.view.ViewGroup} to determine how this view should be 1625 * laid out. 1626 * {@hide} 1627 */ 1628 protected ViewGroup.LayoutParams mLayoutParams; 1629 1630 /** 1631 * The view flags hold various views states. 1632 * {@hide} 1633 */ 1634 @ViewDebug.ExportedProperty 1635 int mViewFlags; 1636 1637 /** 1638 * The distance in pixels from the left edge of this view's parent 1639 * to the left edge of this view. 1640 * {@hide} 1641 */ 1642 @ViewDebug.ExportedProperty(category = "layout") 1643 protected int mLeft; 1644 /** 1645 * The distance in pixels from the left edge of this view's parent 1646 * to the right edge of this view. 1647 * {@hide} 1648 */ 1649 @ViewDebug.ExportedProperty(category = "layout") 1650 protected int mRight; 1651 /** 1652 * The distance in pixels from the top edge of this view's parent 1653 * to the top edge of this view. 1654 * {@hide} 1655 */ 1656 @ViewDebug.ExportedProperty(category = "layout") 1657 protected int mTop; 1658 /** 1659 * The distance in pixels from the top edge of this view's parent 1660 * to the bottom edge of this view. 1661 * {@hide} 1662 */ 1663 @ViewDebug.ExportedProperty(category = "layout") 1664 protected int mBottom; 1665 1666 /** 1667 * The offset, in pixels, by which the content of this view is scrolled 1668 * horizontally. 1669 * {@hide} 1670 */ 1671 @ViewDebug.ExportedProperty(category = "scrolling") 1672 protected int mScrollX; 1673 /** 1674 * The offset, in pixels, by which the content of this view is scrolled 1675 * vertically. 1676 * {@hide} 1677 */ 1678 @ViewDebug.ExportedProperty(category = "scrolling") 1679 protected int mScrollY; 1680 1681 /** 1682 * The left padding in pixels, that is the distance in pixels between the 1683 * left edge of this view and the left edge of its content. 1684 * {@hide} 1685 */ 1686 @ViewDebug.ExportedProperty(category = "padding") 1687 protected int mPaddingLeft; 1688 /** 1689 * The right padding in pixels, that is the distance in pixels between the 1690 * right edge of this view and the right edge of its content. 1691 * {@hide} 1692 */ 1693 @ViewDebug.ExportedProperty(category = "padding") 1694 protected int mPaddingRight; 1695 /** 1696 * The top padding in pixels, that is the distance in pixels between the 1697 * top edge of this view and the top edge of its content. 1698 * {@hide} 1699 */ 1700 @ViewDebug.ExportedProperty(category = "padding") 1701 protected int mPaddingTop; 1702 /** 1703 * The bottom padding in pixels, that is the distance in pixels between the 1704 * bottom edge of this view and the bottom edge of its content. 1705 * {@hide} 1706 */ 1707 @ViewDebug.ExportedProperty(category = "padding") 1708 protected int mPaddingBottom; 1709 1710 /** 1711 * Briefly describes the view and is primarily used for accessibility support. 1712 */ 1713 private CharSequence mContentDescription; 1714 1715 /** 1716 * Cache the paddingRight set by the user to append to the scrollbar's size. 1717 */ 1718 @ViewDebug.ExportedProperty(category = "padding") 1719 int mUserPaddingRight; 1720 1721 /** 1722 * Cache the paddingBottom set by the user to append to the scrollbar's size. 1723 */ 1724 @ViewDebug.ExportedProperty(category = "padding") 1725 int mUserPaddingBottom; 1726 1727 /** 1728 * @hide 1729 */ 1730 int mOldWidthMeasureSpec = Integer.MIN_VALUE; 1731 /** 1732 * @hide 1733 */ 1734 int mOldHeightMeasureSpec = Integer.MIN_VALUE; 1735 1736 private Resources mResources = null; 1737 1738 private Drawable mBGDrawable; 1739 1740 private int mBackgroundResource; 1741 private boolean mBackgroundSizeChanged; 1742 1743 /** 1744 * Listener used to dispatch focus change events. 1745 * This field should be made private, so it is hidden from the SDK. 1746 * {@hide} 1747 */ 1748 protected OnFocusChangeListener mOnFocusChangeListener; 1749 1750 /** 1751 * Listener used to dispatch click events. 1752 * This field should be made private, so it is hidden from the SDK. 1753 * {@hide} 1754 */ 1755 protected OnClickListener mOnClickListener; 1756 1757 /** 1758 * Listener used to dispatch long click events. 1759 * This field should be made private, so it is hidden from the SDK. 1760 * {@hide} 1761 */ 1762 protected OnLongClickListener mOnLongClickListener; 1763 1764 /** 1765 * Listener used to build the context menu. 1766 * This field should be made private, so it is hidden from the SDK. 1767 * {@hide} 1768 */ 1769 protected OnCreateContextMenuListener mOnCreateContextMenuListener; 1770 1771 private OnKeyListener mOnKeyListener; 1772 1773 private OnTouchListener mOnTouchListener; 1774 1775 /** 1776 * The application environment this view lives in. 1777 * This field should be made private, so it is hidden from the SDK. 1778 * {@hide} 1779 */ 1780 protected Context mContext; 1781 1782 private ScrollabilityCache mScrollCache; 1783 1784 private int[] mDrawableState = null; 1785 1786 private SoftReference<Bitmap> mDrawingCache; 1787 private SoftReference<Bitmap> mUnscaledDrawingCache; 1788 1789 /** 1790 * When this view has focus and the next focus is {@link #FOCUS_LEFT}, 1791 * the user may specify which view to go to next. 1792 */ 1793 private int mNextFocusLeftId = View.NO_ID; 1794 1795 /** 1796 * When this view has focus and the next focus is {@link #FOCUS_RIGHT}, 1797 * the user may specify which view to go to next. 1798 */ 1799 private int mNextFocusRightId = View.NO_ID; 1800 1801 /** 1802 * When this view has focus and the next focus is {@link #FOCUS_UP}, 1803 * the user may specify which view to go to next. 1804 */ 1805 private int mNextFocusUpId = View.NO_ID; 1806 1807 /** 1808 * When this view has focus and the next focus is {@link #FOCUS_DOWN}, 1809 * the user may specify which view to go to next. 1810 */ 1811 private int mNextFocusDownId = View.NO_ID; 1812 1813 private CheckForLongPress mPendingCheckForLongPress; 1814 private CheckForTap mPendingCheckForTap = null; 1815 private PerformClick mPerformClick; 1816 1817 private UnsetPressedState mUnsetPressedState; 1818 1819 /** 1820 * Whether the long press's action has been invoked. The tap's action is invoked on the 1821 * up event while a long press is invoked as soon as the long press duration is reached, so 1822 * a long press could be performed before the tap is checked, in which case the tap's action 1823 * should not be invoked. 1824 */ 1825 private boolean mHasPerformedLongPress; 1826 1827 /** 1828 * The minimum height of the view. We'll try our best to have the height 1829 * of this view to at least this amount. 1830 */ 1831 @ViewDebug.ExportedProperty(category = "measurement") 1832 private int mMinHeight; 1833 1834 /** 1835 * The minimum width of the view. We'll try our best to have the width 1836 * of this view to at least this amount. 1837 */ 1838 @ViewDebug.ExportedProperty(category = "measurement") 1839 private int mMinWidth; 1840 1841 /** 1842 * The delegate to handle touch events that are physically in this view 1843 * but should be handled by another view. 1844 */ 1845 private TouchDelegate mTouchDelegate = null; 1846 1847 /** 1848 * Solid color to use as a background when creating the drawing cache. Enables 1849 * the cache to use 16 bit bitmaps instead of 32 bit. 1850 */ 1851 private int mDrawingCacheBackgroundColor = 0; 1852 1853 /** 1854 * Special tree observer used when mAttachInfo is null. 1855 */ 1856 private ViewTreeObserver mFloatingTreeObserver; 1857 1858 /** 1859 * Cache the touch slop from the context that created the view. 1860 */ 1861 private int mTouchSlop; 1862 1863 // Used for debug only 1864 static long sInstanceCount = 0; 1865 1866 /** 1867 * Simple constructor to use when creating a view from code. 1868 * 1869 * @param context The Context the view is running in, through which it can 1870 * access the current theme, resources, etc. 1871 */ 1872 public View(Context context) { 1873 mContext = context; 1874 mResources = context != null ? context.getResources() : null; 1875 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED; 1876 // Used for debug only 1877 //++sInstanceCount; 1878 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); 1879 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS); 1880 } 1881 1882 /** 1883 * Constructor that is called when inflating a view from XML. This is called 1884 * when a view is being constructed from an XML file, supplying attributes 1885 * that were specified in the XML file. This version uses a default style of 1886 * 0, so the only attribute values applied are those in the Context's Theme 1887 * and the given AttributeSet. 1888 * 1889 * <p> 1890 * The method onFinishInflate() will be called after all children have been 1891 * added. 1892 * 1893 * @param context The Context the view is running in, through which it can 1894 * access the current theme, resources, etc. 1895 * @param attrs The attributes of the XML tag that is inflating the view. 1896 * @see #View(Context, AttributeSet, int) 1897 */ 1898 public View(Context context, AttributeSet attrs) { 1899 this(context, attrs, 0); 1900 } 1901 1902 /** 1903 * Perform inflation from XML and apply a class-specific base style. This 1904 * constructor of View allows subclasses to use their own base style when 1905 * they are inflating. For example, a Button class's constructor would call 1906 * this version of the super class constructor and supply 1907 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows 1908 * the theme's button style to modify all of the base view attributes (in 1909 * particular its background) as well as the Button class's attributes. 1910 * 1911 * @param context The Context the view is running in, through which it can 1912 * access the current theme, resources, etc. 1913 * @param attrs The attributes of the XML tag that is inflating the view. 1914 * @param defStyle The default style to apply to this view. If 0, no style 1915 * will be applied (beyond what is included in the theme). This may 1916 * either be an attribute resource, whose value will be retrieved 1917 * from the current theme, or an explicit style resource. 1918 * @see #View(Context, AttributeSet) 1919 */ 1920 public View(Context context, AttributeSet attrs, int defStyle) { 1921 this(context); 1922 1923 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, 1924 defStyle, 0); 1925 1926 Drawable background = null; 1927 1928 int leftPadding = -1; 1929 int topPadding = -1; 1930 int rightPadding = -1; 1931 int bottomPadding = -1; 1932 1933 int padding = -1; 1934 1935 int viewFlagValues = 0; 1936 int viewFlagMasks = 0; 1937 1938 boolean setScrollContainer = false; 1939 1940 int x = 0; 1941 int y = 0; 1942 1943 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY; 1944 1945 int overScrollMode = mOverScrollMode; 1946 final int N = a.getIndexCount(); 1947 for (int i = 0; i < N; i++) { 1948 int attr = a.getIndex(i); 1949 switch (attr) { 1950 case com.android.internal.R.styleable.View_background: 1951 background = a.getDrawable(attr); 1952 break; 1953 case com.android.internal.R.styleable.View_padding: 1954 padding = a.getDimensionPixelSize(attr, -1); 1955 break; 1956 case com.android.internal.R.styleable.View_paddingLeft: 1957 leftPadding = a.getDimensionPixelSize(attr, -1); 1958 break; 1959 case com.android.internal.R.styleable.View_paddingTop: 1960 topPadding = a.getDimensionPixelSize(attr, -1); 1961 break; 1962 case com.android.internal.R.styleable.View_paddingRight: 1963 rightPadding = a.getDimensionPixelSize(attr, -1); 1964 break; 1965 case com.android.internal.R.styleable.View_paddingBottom: 1966 bottomPadding = a.getDimensionPixelSize(attr, -1); 1967 break; 1968 case com.android.internal.R.styleable.View_scrollX: 1969 x = a.getDimensionPixelOffset(attr, 0); 1970 break; 1971 case com.android.internal.R.styleable.View_scrollY: 1972 y = a.getDimensionPixelOffset(attr, 0); 1973 break; 1974 case com.android.internal.R.styleable.View_id: 1975 mID = a.getResourceId(attr, NO_ID); 1976 break; 1977 case com.android.internal.R.styleable.View_tag: 1978 mTag = a.getText(attr); 1979 break; 1980 case com.android.internal.R.styleable.View_fitsSystemWindows: 1981 if (a.getBoolean(attr, false)) { 1982 viewFlagValues |= FITS_SYSTEM_WINDOWS; 1983 viewFlagMasks |= FITS_SYSTEM_WINDOWS; 1984 } 1985 break; 1986 case com.android.internal.R.styleable.View_focusable: 1987 if (a.getBoolean(attr, false)) { 1988 viewFlagValues |= FOCUSABLE; 1989 viewFlagMasks |= FOCUSABLE_MASK; 1990 } 1991 break; 1992 case com.android.internal.R.styleable.View_focusableInTouchMode: 1993 if (a.getBoolean(attr, false)) { 1994 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE; 1995 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK; 1996 } 1997 break; 1998 case com.android.internal.R.styleable.View_clickable: 1999 if (a.getBoolean(attr, false)) { 2000 viewFlagValues |= CLICKABLE; 2001 viewFlagMasks |= CLICKABLE; 2002 } 2003 break; 2004 case com.android.internal.R.styleable.View_longClickable: 2005 if (a.getBoolean(attr, false)) { 2006 viewFlagValues |= LONG_CLICKABLE; 2007 viewFlagMasks |= LONG_CLICKABLE; 2008 } 2009 break; 2010 case com.android.internal.R.styleable.View_saveEnabled: 2011 if (!a.getBoolean(attr, true)) { 2012 viewFlagValues |= SAVE_DISABLED; 2013 viewFlagMasks |= SAVE_DISABLED_MASK; 2014 } 2015 break; 2016 case com.android.internal.R.styleable.View_duplicateParentState: 2017 if (a.getBoolean(attr, false)) { 2018 viewFlagValues |= DUPLICATE_PARENT_STATE; 2019 viewFlagMasks |= DUPLICATE_PARENT_STATE; 2020 } 2021 break; 2022 case com.android.internal.R.styleable.View_visibility: 2023 final int visibility = a.getInt(attr, 0); 2024 if (visibility != 0) { 2025 viewFlagValues |= VISIBILITY_FLAGS[visibility]; 2026 viewFlagMasks |= VISIBILITY_MASK; 2027 } 2028 break; 2029 case com.android.internal.R.styleable.View_drawingCacheQuality: 2030 final int cacheQuality = a.getInt(attr, 0); 2031 if (cacheQuality != 0) { 2032 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality]; 2033 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK; 2034 } 2035 break; 2036 case com.android.internal.R.styleable.View_contentDescription: 2037 mContentDescription = a.getString(attr); 2038 break; 2039 case com.android.internal.R.styleable.View_soundEffectsEnabled: 2040 if (!a.getBoolean(attr, true)) { 2041 viewFlagValues &= ~SOUND_EFFECTS_ENABLED; 2042 viewFlagMasks |= SOUND_EFFECTS_ENABLED; 2043 } 2044 break; 2045 case com.android.internal.R.styleable.View_hapticFeedbackEnabled: 2046 if (!a.getBoolean(attr, true)) { 2047 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED; 2048 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED; 2049 } 2050 break; 2051 case R.styleable.View_scrollbars: 2052 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE); 2053 if (scrollbars != SCROLLBARS_NONE) { 2054 viewFlagValues |= scrollbars; 2055 viewFlagMasks |= SCROLLBARS_MASK; 2056 initializeScrollbars(a); 2057 } 2058 break; 2059 case R.styleable.View_fadingEdge: 2060 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE); 2061 if (fadingEdge != FADING_EDGE_NONE) { 2062 viewFlagValues |= fadingEdge; 2063 viewFlagMasks |= FADING_EDGE_MASK; 2064 initializeFadingEdge(a); 2065 } 2066 break; 2067 case R.styleable.View_scrollbarStyle: 2068 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY); 2069 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) { 2070 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK; 2071 viewFlagMasks |= SCROLLBARS_STYLE_MASK; 2072 } 2073 break; 2074 case R.styleable.View_isScrollContainer: 2075 setScrollContainer = true; 2076 if (a.getBoolean(attr, false)) { 2077 setScrollContainer(true); 2078 } 2079 break; 2080 case com.android.internal.R.styleable.View_keepScreenOn: 2081 if (a.getBoolean(attr, false)) { 2082 viewFlagValues |= KEEP_SCREEN_ON; 2083 viewFlagMasks |= KEEP_SCREEN_ON; 2084 } 2085 break; 2086 case R.styleable.View_filterTouchesWhenObscured: 2087 if (a.getBoolean(attr, false)) { 2088 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED; 2089 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED; 2090 } 2091 break; 2092 case R.styleable.View_nextFocusLeft: 2093 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID); 2094 break; 2095 case R.styleable.View_nextFocusRight: 2096 mNextFocusRightId = a.getResourceId(attr, View.NO_ID); 2097 break; 2098 case R.styleable.View_nextFocusUp: 2099 mNextFocusUpId = a.getResourceId(attr, View.NO_ID); 2100 break; 2101 case R.styleable.View_nextFocusDown: 2102 mNextFocusDownId = a.getResourceId(attr, View.NO_ID); 2103 break; 2104 case R.styleable.View_minWidth: 2105 mMinWidth = a.getDimensionPixelSize(attr, 0); 2106 break; 2107 case R.styleable.View_minHeight: 2108 mMinHeight = a.getDimensionPixelSize(attr, 0); 2109 break; 2110 case R.styleable.View_onClick: 2111 if (context.isRestricted()) { 2112 throw new IllegalStateException("The android:onClick attribute cannot " 2113 + "be used within a restricted context"); 2114 } 2115 2116 final String handlerName = a.getString(attr); 2117 if (handlerName != null) { 2118 setOnClickListener(new OnClickListener() { 2119 private Method mHandler; 2120 2121 public void onClick(View v) { 2122 if (mHandler == null) { 2123 try { 2124 mHandler = getContext().getClass().getMethod(handlerName, 2125 View.class); 2126 } catch (NoSuchMethodException e) { 2127 int id = getId(); 2128 String idText = id == NO_ID ? "" : " with id '" 2129 + getContext().getResources().getResourceEntryName( 2130 id) + "'"; 2131 throw new IllegalStateException("Could not find a method " + 2132 handlerName + "(View) in the activity " 2133 + getContext().getClass() + " for onClick handler" 2134 + " on view " + View.this.getClass() + idText, e); 2135 } 2136 } 2137 2138 try { 2139 mHandler.invoke(getContext(), View.this); 2140 } catch (IllegalAccessException e) { 2141 throw new IllegalStateException("Could not execute non " 2142 + "public method of the activity", e); 2143 } catch (InvocationTargetException e) { 2144 throw new IllegalStateException("Could not execute " 2145 + "method of the activity", e); 2146 } 2147 } 2148 }); 2149 } 2150 break; 2151 case R.styleable.View_overScrollMode: 2152 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS); 2153 break; 2154 } 2155 } 2156 2157 setOverScrollMode(overScrollMode); 2158 2159 if (background != null) { 2160 setBackgroundDrawable(background); 2161 } 2162 2163 if (padding >= 0) { 2164 leftPadding = padding; 2165 topPadding = padding; 2166 rightPadding = padding; 2167 bottomPadding = padding; 2168 } 2169 2170 // If the user specified the padding (either with android:padding or 2171 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise 2172 // use the default padding or the padding from the background drawable 2173 // (stored at this point in mPadding*) 2174 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft, 2175 topPadding >= 0 ? topPadding : mPaddingTop, 2176 rightPadding >= 0 ? rightPadding : mPaddingRight, 2177 bottomPadding >= 0 ? bottomPadding : mPaddingBottom); 2178 2179 if (viewFlagMasks != 0) { 2180 setFlags(viewFlagValues, viewFlagMasks); 2181 } 2182 2183 // Needs to be called after mViewFlags is set 2184 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) { 2185 recomputePadding(); 2186 } 2187 2188 if (x != 0 || y != 0) { 2189 scrollTo(x, y); 2190 } 2191 2192 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) { 2193 setScrollContainer(true); 2194 } 2195 2196 computeOpaqueFlags(); 2197 2198 a.recycle(); 2199 } 2200 2201 /** 2202 * Non-public constructor for use in testing 2203 */ 2204 View() { 2205 } 2206 2207 // Used for debug only 2208 /* 2209 @Override 2210 protected void finalize() throws Throwable { 2211 super.finalize(); 2212 --sInstanceCount; 2213 } 2214 */ 2215 2216 /** 2217 * <p> 2218 * Initializes the fading edges from a given set of styled attributes. This 2219 * method should be called by subclasses that need fading edges and when an 2220 * instance of these subclasses is created programmatically rather than 2221 * being inflated from XML. This method is automatically called when the XML 2222 * is inflated. 2223 * </p> 2224 * 2225 * @param a the styled attributes set to initialize the fading edges from 2226 */ 2227 protected void initializeFadingEdge(TypedArray a) { 2228 initScrollCache(); 2229 2230 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize( 2231 R.styleable.View_fadingEdgeLength, 2232 ViewConfiguration.get(mContext).getScaledFadingEdgeLength()); 2233 } 2234 2235 /** 2236 * Returns the size of the vertical faded edges used to indicate that more 2237 * content in this view is visible. 2238 * 2239 * @return The size in pixels of the vertical faded edge or 0 if vertical 2240 * faded edges are not enabled for this view. 2241 * @attr ref android.R.styleable#View_fadingEdgeLength 2242 */ 2243 public int getVerticalFadingEdgeLength() { 2244 if (isVerticalFadingEdgeEnabled()) { 2245 ScrollabilityCache cache = mScrollCache; 2246 if (cache != null) { 2247 return cache.fadingEdgeLength; 2248 } 2249 } 2250 return 0; 2251 } 2252 2253 /** 2254 * Set the size of the faded edge used to indicate that more content in this 2255 * view is available. Will not change whether the fading edge is enabled; use 2256 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled} 2257 * to enable the fading edge for the vertical or horizontal fading edges. 2258 * 2259 * @param length The size in pixels of the faded edge used to indicate that more 2260 * content in this view is visible. 2261 */ 2262 public void setFadingEdgeLength(int length) { 2263 initScrollCache(); 2264 mScrollCache.fadingEdgeLength = length; 2265 } 2266 2267 /** 2268 * Returns the size of the horizontal faded edges used to indicate that more 2269 * content in this view is visible. 2270 * 2271 * @return The size in pixels of the horizontal faded edge or 0 if horizontal 2272 * faded edges are not enabled for this view. 2273 * @attr ref android.R.styleable#View_fadingEdgeLength 2274 */ 2275 public int getHorizontalFadingEdgeLength() { 2276 if (isHorizontalFadingEdgeEnabled()) { 2277 ScrollabilityCache cache = mScrollCache; 2278 if (cache != null) { 2279 return cache.fadingEdgeLength; 2280 } 2281 } 2282 return 0; 2283 } 2284 2285 /** 2286 * Returns the width of the vertical scrollbar. 2287 * 2288 * @return The width in pixels of the vertical scrollbar or 0 if there 2289 * is no vertical scrollbar. 2290 */ 2291 public int getVerticalScrollbarWidth() { 2292 ScrollabilityCache cache = mScrollCache; 2293 if (cache != null) { 2294 ScrollBarDrawable scrollBar = cache.scrollBar; 2295 if (scrollBar != null) { 2296 int size = scrollBar.getSize(true); 2297 if (size <= 0) { 2298 size = cache.scrollBarSize; 2299 } 2300 return size; 2301 } 2302 return 0; 2303 } 2304 return 0; 2305 } 2306 2307 /** 2308 * Returns the height of the horizontal scrollbar. 2309 * 2310 * @return The height in pixels of the horizontal scrollbar or 0 if 2311 * there is no horizontal scrollbar. 2312 */ 2313 protected int getHorizontalScrollbarHeight() { 2314 ScrollabilityCache cache = mScrollCache; 2315 if (cache != null) { 2316 ScrollBarDrawable scrollBar = cache.scrollBar; 2317 if (scrollBar != null) { 2318 int size = scrollBar.getSize(false); 2319 if (size <= 0) { 2320 size = cache.scrollBarSize; 2321 } 2322 return size; 2323 } 2324 return 0; 2325 } 2326 return 0; 2327 } 2328 2329 /** 2330 * <p> 2331 * Initializes the scrollbars from a given set of styled attributes. This 2332 * method should be called by subclasses that need scrollbars and when an 2333 * instance of these subclasses is created programmatically rather than 2334 * being inflated from XML. This method is automatically called when the XML 2335 * is inflated. 2336 * </p> 2337 * 2338 * @param a the styled attributes set to initialize the scrollbars from 2339 */ 2340 protected void initializeScrollbars(TypedArray a) { 2341 initScrollCache(); 2342 2343 final ScrollabilityCache scrollabilityCache = mScrollCache; 2344 2345 if (scrollabilityCache.scrollBar == null) { 2346 scrollabilityCache.scrollBar = new ScrollBarDrawable(); 2347 } 2348 2349 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true); 2350 2351 if (!fadeScrollbars) { 2352 scrollabilityCache.state = ScrollabilityCache.ON; 2353 } 2354 scrollabilityCache.fadeScrollBars = fadeScrollbars; 2355 2356 2357 scrollabilityCache.scrollBarFadeDuration = a.getInt( 2358 R.styleable.View_scrollbarFadeDuration, ViewConfiguration 2359 .getScrollBarFadeDuration()); 2360 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt( 2361 R.styleable.View_scrollbarDefaultDelayBeforeFade, 2362 ViewConfiguration.getScrollDefaultDelay()); 2363 2364 2365 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize( 2366 com.android.internal.R.styleable.View_scrollbarSize, 2367 ViewConfiguration.get(mContext).getScaledScrollBarSize()); 2368 2369 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal); 2370 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track); 2371 2372 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal); 2373 if (thumb != null) { 2374 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb); 2375 } 2376 2377 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack, 2378 false); 2379 if (alwaysDraw) { 2380 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true); 2381 } 2382 2383 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical); 2384 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track); 2385 2386 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical); 2387 if (thumb != null) { 2388 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb); 2389 } 2390 2391 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack, 2392 false); 2393 if (alwaysDraw) { 2394 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true); 2395 } 2396 2397 // Re-apply user/background padding so that scrollbar(s) get added 2398 recomputePadding(); 2399 } 2400 2401 /** 2402 * <p> 2403 * Initalizes the scrollability cache if necessary. 2404 * </p> 2405 */ 2406 private void initScrollCache() { 2407 if (mScrollCache == null) { 2408 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this); 2409 } 2410 } 2411 2412 /** 2413 * Register a callback to be invoked when focus of this view changed. 2414 * 2415 * @param l The callback that will run. 2416 */ 2417 public void setOnFocusChangeListener(OnFocusChangeListener l) { 2418 mOnFocusChangeListener = l; 2419 } 2420 2421 /** 2422 * Returns the focus-change callback registered for this view. 2423 * 2424 * @return The callback, or null if one is not registered. 2425 */ 2426 public OnFocusChangeListener getOnFocusChangeListener() { 2427 return mOnFocusChangeListener; 2428 } 2429 2430 /** 2431 * Register a callback to be invoked when this view is clicked. If this view is not 2432 * clickable, it becomes clickable. 2433 * 2434 * @param l The callback that will run 2435 * 2436 * @see #setClickable(boolean) 2437 */ 2438 public void setOnClickListener(OnClickListener l) { 2439 if (!isClickable()) { 2440 setClickable(true); 2441 } 2442 mOnClickListener = l; 2443 } 2444 2445 /** 2446 * Register a callback to be invoked when this view is clicked and held. If this view is not 2447 * long clickable, it becomes long clickable. 2448 * 2449 * @param l The callback that will run 2450 * 2451 * @see #setLongClickable(boolean) 2452 */ 2453 public void setOnLongClickListener(OnLongClickListener l) { 2454 if (!isLongClickable()) { 2455 setLongClickable(true); 2456 } 2457 mOnLongClickListener = l; 2458 } 2459 2460 /** 2461 * Register a callback to be invoked when the context menu for this view is 2462 * being built. If this view is not long clickable, it becomes long clickable. 2463 * 2464 * @param l The callback that will run 2465 * 2466 */ 2467 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) { 2468 if (!isLongClickable()) { 2469 setLongClickable(true); 2470 } 2471 mOnCreateContextMenuListener = l; 2472 } 2473 2474 /** 2475 * Call this view's OnClickListener, if it is defined. 2476 * 2477 * @return True there was an assigned OnClickListener that was called, false 2478 * otherwise is returned. 2479 */ 2480 public boolean performClick() { 2481 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); 2482 2483 if (mOnClickListener != null) { 2484 playSoundEffect(SoundEffectConstants.CLICK); 2485 mOnClickListener.onClick(this); 2486 return true; 2487 } 2488 2489 return false; 2490 } 2491 2492 /** 2493 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the 2494 * OnLongClickListener did not consume the event. 2495 * 2496 * @return True if one of the above receivers consumed the event, false otherwise. 2497 */ 2498 public boolean performLongClick() { 2499 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED); 2500 2501 boolean handled = false; 2502 if (mOnLongClickListener != null) { 2503 handled = mOnLongClickListener.onLongClick(View.this); 2504 } 2505 if (!handled) { 2506 handled = showContextMenu(); 2507 } 2508 if (handled) { 2509 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); 2510 } 2511 return handled; 2512 } 2513 2514 /** 2515 * Bring up the context menu for this view. 2516 * 2517 * @return Whether a context menu was displayed. 2518 */ 2519 public boolean showContextMenu() { 2520 return getParent().showContextMenuForChild(this); 2521 } 2522 2523 /** 2524 * Register a callback to be invoked when a key is pressed in this view. 2525 * @param l the key listener to attach to this view 2526 */ 2527 public void setOnKeyListener(OnKeyListener l) { 2528 mOnKeyListener = l; 2529 } 2530 2531 /** 2532 * Register a callback to be invoked when a touch event is sent to this view. 2533 * @param l the touch listener to attach to this view 2534 */ 2535 public void setOnTouchListener(OnTouchListener l) { 2536 mOnTouchListener = l; 2537 } 2538 2539 /** 2540 * Give this view focus. This will cause {@link #onFocusChanged} to be called. 2541 * 2542 * Note: this does not check whether this {@link View} should get focus, it just 2543 * gives it focus no matter what. It should only be called internally by framework 2544 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}. 2545 * 2546 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN, 2547 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which 2548 * focus moved when requestFocus() is called. It may not always 2549 * apply, in which case use the default View.FOCUS_DOWN. 2550 * @param previouslyFocusedRect The rectangle of the view that had focus 2551 * prior in this View's coordinate system. 2552 */ 2553 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { 2554 if (DBG) { 2555 System.out.println(this + " requestFocus()"); 2556 } 2557 2558 if ((mPrivateFlags & FOCUSED) == 0) { 2559 mPrivateFlags |= FOCUSED; 2560 2561 if (mParent != null) { 2562 mParent.requestChildFocus(this, this); 2563 } 2564 2565 onFocusChanged(true, direction, previouslyFocusedRect); 2566 refreshDrawableState(); 2567 } 2568 } 2569 2570 /** 2571 * Request that a rectangle of this view be visible on the screen, 2572 * scrolling if necessary just enough. 2573 * 2574 * <p>A View should call this if it maintains some notion of which part 2575 * of its content is interesting. For example, a text editing view 2576 * should call this when its cursor moves. 2577 * 2578 * @param rectangle The rectangle. 2579 * @return Whether any parent scrolled. 2580 */ 2581 public boolean requestRectangleOnScreen(Rect rectangle) { 2582 return requestRectangleOnScreen(rectangle, false); 2583 } 2584 2585 /** 2586 * Request that a rectangle of this view be visible on the screen, 2587 * scrolling if necessary just enough. 2588 * 2589 * <p>A View should call this if it maintains some notion of which part 2590 * of its content is interesting. For example, a text editing view 2591 * should call this when its cursor moves. 2592 * 2593 * <p>When <code>immediate</code> is set to true, scrolling will not be 2594 * animated. 2595 * 2596 * @param rectangle The rectangle. 2597 * @param immediate True to forbid animated scrolling, false otherwise 2598 * @return Whether any parent scrolled. 2599 */ 2600 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) { 2601 View child = this; 2602 ViewParent parent = mParent; 2603 boolean scrolled = false; 2604 while (parent != null) { 2605 scrolled |= parent.requestChildRectangleOnScreen(child, 2606 rectangle, immediate); 2607 2608 // offset rect so next call has the rectangle in the 2609 // coordinate system of its direct child. 2610 rectangle.offset(child.getLeft(), child.getTop()); 2611 rectangle.offset(-child.getScrollX(), -child.getScrollY()); 2612 2613 if (!(parent instanceof View)) { 2614 break; 2615 } 2616 2617 child = (View) parent; 2618 parent = child.getParent(); 2619 } 2620 return scrolled; 2621 } 2622 2623 /** 2624 * Called when this view wants to give up focus. This will cause 2625 * {@link #onFocusChanged} to be called. 2626 */ 2627 public void clearFocus() { 2628 if (DBG) { 2629 System.out.println(this + " clearFocus()"); 2630 } 2631 2632 if ((mPrivateFlags & FOCUSED) != 0) { 2633 mPrivateFlags &= ~FOCUSED; 2634 2635 if (mParent != null) { 2636 mParent.clearChildFocus(this); 2637 } 2638 2639 onFocusChanged(false, 0, null); 2640 refreshDrawableState(); 2641 } 2642 } 2643 2644 /** 2645 * Called to clear the focus of a view that is about to be removed. 2646 * Doesn't call clearChildFocus, which prevents this view from taking 2647 * focus again before it has been removed from the parent 2648 */ 2649 void clearFocusForRemoval() { 2650 if ((mPrivateFlags & FOCUSED) != 0) { 2651 mPrivateFlags &= ~FOCUSED; 2652 2653 onFocusChanged(false, 0, null); 2654 refreshDrawableState(); 2655 } 2656 } 2657 2658 /** 2659 * Called internally by the view system when a new view is getting focus. 2660 * This is what clears the old focus. 2661 */ 2662 void unFocus() { 2663 if (DBG) { 2664 System.out.println(this + " unFocus()"); 2665 } 2666 2667 if ((mPrivateFlags & FOCUSED) != 0) { 2668 mPrivateFlags &= ~FOCUSED; 2669 2670 onFocusChanged(false, 0, null); 2671 refreshDrawableState(); 2672 } 2673 } 2674 2675 /** 2676 * Returns true if this view has focus iteself, or is the ancestor of the 2677 * view that has focus. 2678 * 2679 * @return True if this view has or contains focus, false otherwise. 2680 */ 2681 @ViewDebug.ExportedProperty(category = "focus") 2682 public boolean hasFocus() { 2683 return (mPrivateFlags & FOCUSED) != 0; 2684 } 2685 2686 /** 2687 * Returns true if this view is focusable or if it contains a reachable View 2688 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()" 2689 * is a View whose parents do not block descendants focus. 2690 * 2691 * Only {@link #VISIBLE} views are considered focusable. 2692 * 2693 * @return True if the view is focusable or if the view contains a focusable 2694 * View, false otherwise. 2695 * 2696 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS 2697 */ 2698 public boolean hasFocusable() { 2699 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable(); 2700 } 2701 2702 /** 2703 * Called by the view system when the focus state of this view changes. 2704 * When the focus change event is caused by directional navigation, direction 2705 * and previouslyFocusedRect provide insight into where the focus is coming from. 2706 * When overriding, be sure to call up through to the super class so that 2707 * the standard focus handling will occur. 2708 * 2709 * @param gainFocus True if the View has focus; false otherwise. 2710 * @param direction The direction focus has moved when requestFocus() 2711 * is called to give this view focus. Values are 2712 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT} or 2713 * {@link #FOCUS_RIGHT}. It may not always apply, in which 2714 * case use the default. 2715 * @param previouslyFocusedRect The rectangle, in this view's coordinate 2716 * system, of the previously focused view. If applicable, this will be 2717 * passed in as finer grained information about where the focus is coming 2718 * from (in addition to direction). Will be <code>null</code> otherwise. 2719 */ 2720 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { 2721 if (gainFocus) { 2722 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED); 2723 } 2724 2725 InputMethodManager imm = InputMethodManager.peekInstance(); 2726 if (!gainFocus) { 2727 if (isPressed()) { 2728 setPressed(false); 2729 } 2730 if (imm != null && mAttachInfo != null 2731 && mAttachInfo.mHasWindowFocus) { 2732 imm.focusOut(this); 2733 } 2734 onFocusLost(); 2735 } else if (imm != null && mAttachInfo != null 2736 && mAttachInfo.mHasWindowFocus) { 2737 imm.focusIn(this); 2738 } 2739 2740 invalidate(); 2741 if (mOnFocusChangeListener != null) { 2742 mOnFocusChangeListener.onFocusChange(this, gainFocus); 2743 } 2744 2745 if (mAttachInfo != null) { 2746 mAttachInfo.mKeyDispatchState.reset(this); 2747 } 2748 } 2749 2750 /** 2751 * {@inheritDoc} 2752 */ 2753 public void sendAccessibilityEvent(int eventType) { 2754 if (AccessibilityManager.getInstance(mContext).isEnabled()) { 2755 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType)); 2756 } 2757 } 2758 2759 /** 2760 * {@inheritDoc} 2761 */ 2762 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) { 2763 event.setClassName(getClass().getName()); 2764 event.setPackageName(getContext().getPackageName()); 2765 event.setEnabled(isEnabled()); 2766 event.setContentDescription(mContentDescription); 2767 2768 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) { 2769 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList; 2770 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL); 2771 event.setItemCount(focusablesTempList.size()); 2772 event.setCurrentItemIndex(focusablesTempList.indexOf(this)); 2773 focusablesTempList.clear(); 2774 } 2775 2776 dispatchPopulateAccessibilityEvent(event); 2777 2778 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event); 2779 } 2780 2781 /** 2782 * Dispatches an {@link AccessibilityEvent} to the {@link View} children 2783 * to be populated. 2784 * 2785 * @param event The event. 2786 * 2787 * @return True if the event population was completed. 2788 */ 2789 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 2790 return false; 2791 } 2792 2793 /** 2794 * Gets the {@link View} description. It briefly describes the view and is 2795 * primarily used for accessibility support. Set this property to enable 2796 * better accessibility support for your application. This is especially 2797 * true for views that do not have textual representation (For example, 2798 * ImageButton). 2799 * 2800 * @return The content descriptiopn. 2801 * 2802 * @attr ref android.R.styleable#View_contentDescription 2803 */ 2804 public CharSequence getContentDescription() { 2805 return mContentDescription; 2806 } 2807 2808 /** 2809 * Sets the {@link View} description. It briefly describes the view and is 2810 * primarily used for accessibility support. Set this property to enable 2811 * better accessibility support for your application. This is especially 2812 * true for views that do not have textual representation (For example, 2813 * ImageButton). 2814 * 2815 * @param contentDescription The content description. 2816 * 2817 * @attr ref android.R.styleable#View_contentDescription 2818 */ 2819 public void setContentDescription(CharSequence contentDescription) { 2820 mContentDescription = contentDescription; 2821 } 2822 2823 /** 2824 * Invoked whenever this view loses focus, either by losing window focus or by losing 2825 * focus within its window. This method can be used to clear any state tied to the 2826 * focus. For instance, if a button is held pressed with the trackball and the window 2827 * loses focus, this method can be used to cancel the press. 2828 * 2829 * Subclasses of View overriding this method should always call super.onFocusLost(). 2830 * 2831 * @see #onFocusChanged(boolean, int, android.graphics.Rect) 2832 * @see #onWindowFocusChanged(boolean) 2833 * 2834 * @hide pending API council approval 2835 */ 2836 protected void onFocusLost() { 2837 resetPressedState(); 2838 } 2839 2840 private void resetPressedState() { 2841 if ((mViewFlags & ENABLED_MASK) == DISABLED) { 2842 return; 2843 } 2844 2845 if (isPressed()) { 2846 setPressed(false); 2847 2848 if (!mHasPerformedLongPress) { 2849 removeLongPressCallback(); 2850 } 2851 } 2852 } 2853 2854 /** 2855 * Returns true if this view has focus 2856 * 2857 * @return True if this view has focus, false otherwise. 2858 */ 2859 @ViewDebug.ExportedProperty(category = "focus") 2860 public boolean isFocused() { 2861 return (mPrivateFlags & FOCUSED) != 0; 2862 } 2863 2864 /** 2865 * Find the view in the hierarchy rooted at this view that currently has 2866 * focus. 2867 * 2868 * @return The view that currently has focus, or null if no focused view can 2869 * be found. 2870 */ 2871 public View findFocus() { 2872 return (mPrivateFlags & FOCUSED) != 0 ? this : null; 2873 } 2874 2875 /** 2876 * Change whether this view is one of the set of scrollable containers in 2877 * its window. This will be used to determine whether the window can 2878 * resize or must pan when a soft input area is open -- scrollable 2879 * containers allow the window to use resize mode since the container 2880 * will appropriately shrink. 2881 */ 2882 public void setScrollContainer(boolean isScrollContainer) { 2883 if (isScrollContainer) { 2884 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) { 2885 mAttachInfo.mScrollContainers.add(this); 2886 mPrivateFlags |= SCROLL_CONTAINER_ADDED; 2887 } 2888 mPrivateFlags |= SCROLL_CONTAINER; 2889 } else { 2890 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) { 2891 mAttachInfo.mScrollContainers.remove(this); 2892 } 2893 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED); 2894 } 2895 } 2896 2897 /** 2898 * Returns the quality of the drawing cache. 2899 * 2900 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO}, 2901 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH} 2902 * 2903 * @see #setDrawingCacheQuality(int) 2904 * @see #setDrawingCacheEnabled(boolean) 2905 * @see #isDrawingCacheEnabled() 2906 * 2907 * @attr ref android.R.styleable#View_drawingCacheQuality 2908 */ 2909 public int getDrawingCacheQuality() { 2910 return mViewFlags & DRAWING_CACHE_QUALITY_MASK; 2911 } 2912 2913 /** 2914 * Set the drawing cache quality of this view. This value is used only when the 2915 * drawing cache is enabled 2916 * 2917 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO}, 2918 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH} 2919 * 2920 * @see #getDrawingCacheQuality() 2921 * @see #setDrawingCacheEnabled(boolean) 2922 * @see #isDrawingCacheEnabled() 2923 * 2924 * @attr ref android.R.styleable#View_drawingCacheQuality 2925 */ 2926 public void setDrawingCacheQuality(int quality) { 2927 setFlags(quality, DRAWING_CACHE_QUALITY_MASK); 2928 } 2929 2930 /** 2931 * Returns whether the screen should remain on, corresponding to the current 2932 * value of {@link #KEEP_SCREEN_ON}. 2933 * 2934 * @return Returns true if {@link #KEEP_SCREEN_ON} is set. 2935 * 2936 * @see #setKeepScreenOn(boolean) 2937 * 2938 * @attr ref android.R.styleable#View_keepScreenOn 2939 */ 2940 public boolean getKeepScreenOn() { 2941 return (mViewFlags & KEEP_SCREEN_ON) != 0; 2942 } 2943 2944 /** 2945 * Controls whether the screen should remain on, modifying the 2946 * value of {@link #KEEP_SCREEN_ON}. 2947 * 2948 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}. 2949 * 2950 * @see #getKeepScreenOn() 2951 * 2952 * @attr ref android.R.styleable#View_keepScreenOn 2953 */ 2954 public void setKeepScreenOn(boolean keepScreenOn) { 2955 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON); 2956 } 2957 2958 /** 2959 * @return The user specified next focus ID. 2960 * 2961 * @attr ref android.R.styleable#View_nextFocusLeft 2962 */ 2963 public int getNextFocusLeftId() { 2964 return mNextFocusLeftId; 2965 } 2966 2967 /** 2968 * Set the id of the view to use for the next focus 2969 * 2970 * @param nextFocusLeftId 2971 * 2972 * @attr ref android.R.styleable#View_nextFocusLeft 2973 */ 2974 public void setNextFocusLeftId(int nextFocusLeftId) { 2975 mNextFocusLeftId = nextFocusLeftId; 2976 } 2977 2978 /** 2979 * @return The user specified next focus ID. 2980 * 2981 * @attr ref android.R.styleable#View_nextFocusRight 2982 */ 2983 public int getNextFocusRightId() { 2984 return mNextFocusRightId; 2985 } 2986 2987 /** 2988 * Set the id of the view to use for the next focus 2989 * 2990 * @param nextFocusRightId 2991 * 2992 * @attr ref android.R.styleable#View_nextFocusRight 2993 */ 2994 public void setNextFocusRightId(int nextFocusRightId) { 2995 mNextFocusRightId = nextFocusRightId; 2996 } 2997 2998 /** 2999 * @return The user specified next focus ID. 3000 * 3001 * @attr ref android.R.styleable#View_nextFocusUp 3002 */ 3003 public int getNextFocusUpId() { 3004 return mNextFocusUpId; 3005 } 3006 3007 /** 3008 * Set the id of the view to use for the next focus 3009 * 3010 * @param nextFocusUpId 3011 * 3012 * @attr ref android.R.styleable#View_nextFocusUp 3013 */ 3014 public void setNextFocusUpId(int nextFocusUpId) { 3015 mNextFocusUpId = nextFocusUpId; 3016 } 3017 3018 /** 3019 * @return The user specified next focus ID. 3020 * 3021 * @attr ref android.R.styleable#View_nextFocusDown 3022 */ 3023 public int getNextFocusDownId() { 3024 return mNextFocusDownId; 3025 } 3026 3027 /** 3028 * Set the id of the view to use for the next focus 3029 * 3030 * @param nextFocusDownId 3031 * 3032 * @attr ref android.R.styleable#View_nextFocusDown 3033 */ 3034 public void setNextFocusDownId(int nextFocusDownId) { 3035 mNextFocusDownId = nextFocusDownId; 3036 } 3037 3038 /** 3039 * Returns the visibility of this view and all of its ancestors 3040 * 3041 * @return True if this view and all of its ancestors are {@link #VISIBLE} 3042 */ 3043 public boolean isShown() { 3044 View current = this; 3045 //noinspection ConstantConditions 3046 do { 3047 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) { 3048 return false; 3049 } 3050 ViewParent parent = current.mParent; 3051 if (parent == null) { 3052 return false; // We are not attached to the view root 3053 } 3054 if (!(parent instanceof View)) { 3055 return true; 3056 } 3057 current = (View) parent; 3058 } while (current != null); 3059 3060 return false; 3061 } 3062 3063 /** 3064 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag 3065 * is set 3066 * 3067 * @param insets Insets for system windows 3068 * 3069 * @return True if this view applied the insets, false otherwise 3070 */ 3071 protected boolean fitSystemWindows(Rect insets) { 3072 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) { 3073 mPaddingLeft = insets.left; 3074 mPaddingTop = insets.top; 3075 mPaddingRight = insets.right; 3076 mPaddingBottom = insets.bottom; 3077 requestLayout(); 3078 return true; 3079 } 3080 return false; 3081 } 3082 3083 /** 3084 * Determine if this view has the FITS_SYSTEM_WINDOWS flag set. 3085 * @return True if window has FITS_SYSTEM_WINDOWS set 3086 * 3087 * @hide 3088 */ 3089 public boolean isFitsSystemWindowsFlagSet() { 3090 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS; 3091 } 3092 3093 /** 3094 * Returns the visibility status for this view. 3095 * 3096 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. 3097 * @attr ref android.R.styleable#View_visibility 3098 */ 3099 @ViewDebug.ExportedProperty(mapping = { 3100 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"), 3101 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"), 3102 @ViewDebug.IntToString(from = GONE, to = "GONE") 3103 }) 3104 public int getVisibility() { 3105 return mViewFlags & VISIBILITY_MASK; 3106 } 3107 3108 /** 3109 * Set the enabled state of this view. 3110 * 3111 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}. 3112 * @attr ref android.R.styleable#View_visibility 3113 */ 3114 @RemotableViewMethod 3115 public void setVisibility(int visibility) { 3116 setFlags(visibility, VISIBILITY_MASK); 3117 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false); 3118 } 3119 3120 /** 3121 * Returns the enabled status for this view. The interpretation of the 3122 * enabled state varies by subclass. 3123 * 3124 * @return True if this view is enabled, false otherwise. 3125 */ 3126 @ViewDebug.ExportedProperty 3127 public boolean isEnabled() { 3128 return (mViewFlags & ENABLED_MASK) == ENABLED; 3129 } 3130 3131 /** 3132 * Set the enabled state of this view. The interpretation of the enabled 3133 * state varies by subclass. 3134 * 3135 * @param enabled True if this view is enabled, false otherwise. 3136 */ 3137 @RemotableViewMethod 3138 public void setEnabled(boolean enabled) { 3139 if (enabled == isEnabled()) return; 3140 3141 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK); 3142 3143 /* 3144 * The View most likely has to change its appearance, so refresh 3145 * the drawable state. 3146 */ 3147 refreshDrawableState(); 3148 3149 // Invalidate too, since the default behavior for views is to be 3150 // be drawn at 50% alpha rather than to change the drawable. 3151 invalidate(); 3152 } 3153 3154 /** 3155 * Set whether this view can receive the focus. 3156 * 3157 * Setting this to false will also ensure that this view is not focusable 3158 * in touch mode. 3159 * 3160 * @param focusable If true, this view can receive the focus. 3161 * 3162 * @see #setFocusableInTouchMode(boolean) 3163 * @attr ref android.R.styleable#View_focusable 3164 */ 3165 public void setFocusable(boolean focusable) { 3166 if (!focusable) { 3167 setFlags(0, FOCUSABLE_IN_TOUCH_MODE); 3168 } 3169 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK); 3170 } 3171 3172 /** 3173 * Set whether this view can receive focus while in touch mode. 3174 * 3175 * Setting this to true will also ensure that this view is focusable. 3176 * 3177 * @param focusableInTouchMode If true, this view can receive the focus while 3178 * in touch mode. 3179 * 3180 * @see #setFocusable(boolean) 3181 * @attr ref android.R.styleable#View_focusableInTouchMode 3182 */ 3183 public void setFocusableInTouchMode(boolean focusableInTouchMode) { 3184 // Focusable in touch mode should always be set before the focusable flag 3185 // otherwise, setting the focusable flag will trigger a focusableViewAvailable() 3186 // which, in touch mode, will not successfully request focus on this view 3187 // because the focusable in touch mode flag is not set 3188 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE); 3189 if (focusableInTouchMode) { 3190 setFlags(FOCUSABLE, FOCUSABLE_MASK); 3191 } 3192 } 3193 3194 /** 3195 * Set whether this view should have sound effects enabled for events such as 3196 * clicking and touching. 3197 * 3198 * <p>You may wish to disable sound effects for a view if you already play sounds, 3199 * for instance, a dial key that plays dtmf tones. 3200 * 3201 * @param soundEffectsEnabled whether sound effects are enabled for this view. 3202 * @see #isSoundEffectsEnabled() 3203 * @see #playSoundEffect(int) 3204 * @attr ref android.R.styleable#View_soundEffectsEnabled 3205 */ 3206 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) { 3207 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED); 3208 } 3209 3210 /** 3211 * @return whether this view should have sound effects enabled for events such as 3212 * clicking and touching. 3213 * 3214 * @see #setSoundEffectsEnabled(boolean) 3215 * @see #playSoundEffect(int) 3216 * @attr ref android.R.styleable#View_soundEffectsEnabled 3217 */ 3218 @ViewDebug.ExportedProperty 3219 public boolean isSoundEffectsEnabled() { 3220 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED); 3221 } 3222 3223 /** 3224 * Set whether this view should have haptic feedback for events such as 3225 * long presses. 3226 * 3227 * <p>You may wish to disable haptic feedback if your view already controls 3228 * its own haptic feedback. 3229 * 3230 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view. 3231 * @see #isHapticFeedbackEnabled() 3232 * @see #performHapticFeedback(int) 3233 * @attr ref android.R.styleable#View_hapticFeedbackEnabled 3234 */ 3235 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) { 3236 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED); 3237 } 3238 3239 /** 3240 * @return whether this view should have haptic feedback enabled for events 3241 * long presses. 3242 * 3243 * @see #setHapticFeedbackEnabled(boolean) 3244 * @see #performHapticFeedback(int) 3245 * @attr ref android.R.styleable#View_hapticFeedbackEnabled 3246 */ 3247 @ViewDebug.ExportedProperty 3248 public boolean isHapticFeedbackEnabled() { 3249 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED); 3250 } 3251 3252 /** 3253 * If this view doesn't do any drawing on its own, set this flag to 3254 * allow further optimizations. By default, this flag is not set on 3255 * View, but could be set on some View subclasses such as ViewGroup. 3256 * 3257 * Typically, if you override {@link #onDraw} you should clear this flag. 3258 * 3259 * @param willNotDraw whether or not this View draw on its own 3260 */ 3261 public void setWillNotDraw(boolean willNotDraw) { 3262 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK); 3263 } 3264 3265 /** 3266 * Returns whether or not this View draws on its own. 3267 * 3268 * @return true if this view has nothing to draw, false otherwise 3269 */ 3270 @ViewDebug.ExportedProperty(category = "drawing") 3271 public boolean willNotDraw() { 3272 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW; 3273 } 3274 3275 /** 3276 * When a View's drawing cache is enabled, drawing is redirected to an 3277 * offscreen bitmap. Some views, like an ImageView, must be able to 3278 * bypass this mechanism if they already draw a single bitmap, to avoid 3279 * unnecessary usage of the memory. 3280 * 3281 * @param willNotCacheDrawing true if this view does not cache its 3282 * drawing, false otherwise 3283 */ 3284 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) { 3285 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING); 3286 } 3287 3288 /** 3289 * Returns whether or not this View can cache its drawing or not. 3290 * 3291 * @return true if this view does not cache its drawing, false otherwise 3292 */ 3293 @ViewDebug.ExportedProperty(category = "drawing") 3294 public boolean willNotCacheDrawing() { 3295 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING; 3296 } 3297 3298 /** 3299 * Indicates whether this view reacts to click events or not. 3300 * 3301 * @return true if the view is clickable, false otherwise 3302 * 3303 * @see #setClickable(boolean) 3304 * @attr ref android.R.styleable#View_clickable 3305 */ 3306 @ViewDebug.ExportedProperty 3307 public boolean isClickable() { 3308 return (mViewFlags & CLICKABLE) == CLICKABLE; 3309 } 3310 3311 /** 3312 * Enables or disables click events for this view. When a view 3313 * is clickable it will change its state to "pressed" on every click. 3314 * Subclasses should set the view clickable to visually react to 3315 * user's clicks. 3316 * 3317 * @param clickable true to make the view clickable, false otherwise 3318 * 3319 * @see #isClickable() 3320 * @attr ref android.R.styleable#View_clickable 3321 */ 3322 public void setClickable(boolean clickable) { 3323 setFlags(clickable ? CLICKABLE : 0, CLICKABLE); 3324 } 3325 3326 /** 3327 * Indicates whether this view reacts to long click events or not. 3328 * 3329 * @return true if the view is long clickable, false otherwise 3330 * 3331 * @see #setLongClickable(boolean) 3332 * @attr ref android.R.styleable#View_longClickable 3333 */ 3334 public boolean isLongClickable() { 3335 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE; 3336 } 3337 3338 /** 3339 * Enables or disables long click events for this view. When a view is long 3340 * clickable it reacts to the user holding down the button for a longer 3341 * duration than a tap. This event can either launch the listener or a 3342 * context menu. 3343 * 3344 * @param longClickable true to make the view long clickable, false otherwise 3345 * @see #isLongClickable() 3346 * @attr ref android.R.styleable#View_longClickable 3347 */ 3348 public void setLongClickable(boolean longClickable) { 3349 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE); 3350 } 3351 3352 /** 3353 * Sets the pressed that for this view. 3354 * 3355 * @see #isClickable() 3356 * @see #setClickable(boolean) 3357 * 3358 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts 3359 * the View's internal state from a previously set "pressed" state. 3360 */ 3361 public void setPressed(boolean pressed) { 3362 if (pressed) { 3363 mPrivateFlags |= PRESSED; 3364 } else { 3365 mPrivateFlags &= ~PRESSED; 3366 } 3367 refreshDrawableState(); 3368 dispatchSetPressed(pressed); 3369 } 3370 3371 /** 3372 * Dispatch setPressed to all of this View's children. 3373 * 3374 * @see #setPressed(boolean) 3375 * 3376 * @param pressed The new pressed state 3377 */ 3378 protected void dispatchSetPressed(boolean pressed) { 3379 } 3380 3381 /** 3382 * Indicates whether the view is currently in pressed state. Unless 3383 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter 3384 * the pressed state. 3385 * 3386 * @see #setPressed 3387 * @see #isClickable() 3388 * @see #setClickable(boolean) 3389 * 3390 * @return true if the view is currently pressed, false otherwise 3391 */ 3392 public boolean isPressed() { 3393 return (mPrivateFlags & PRESSED) == PRESSED; 3394 } 3395 3396 /** 3397 * Indicates whether this view will save its state (that is, 3398 * whether its {@link #onSaveInstanceState} method will be called). 3399 * 3400 * @return Returns true if the view state saving is enabled, else false. 3401 * 3402 * @see #setSaveEnabled(boolean) 3403 * @attr ref android.R.styleable#View_saveEnabled 3404 */ 3405 public boolean isSaveEnabled() { 3406 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED; 3407 } 3408 3409 /** 3410 * Controls whether the saving of this view's state is 3411 * enabled (that is, whether its {@link #onSaveInstanceState} method 3412 * will be called). Note that even if freezing is enabled, the 3413 * view still must have an id assigned to it (via {@link #setId setId()}) 3414 * for its state to be saved. This flag can only disable the 3415 * saving of this view; any child views may still have their state saved. 3416 * 3417 * @param enabled Set to false to <em>disable</em> state saving, or true 3418 * (the default) to allow it. 3419 * 3420 * @see #isSaveEnabled() 3421 * @see #setId(int) 3422 * @see #onSaveInstanceState() 3423 * @attr ref android.R.styleable#View_saveEnabled 3424 */ 3425 public void setSaveEnabled(boolean enabled) { 3426 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK); 3427 } 3428 3429 /** 3430 * Gets whether the framework should discard touches when the view's 3431 * window is obscured by another visible window. 3432 * Refer to the {@link View} security documentation for more details. 3433 * 3434 * @return True if touch filtering is enabled. 3435 * 3436 * @see #setFilterTouchesWhenObscured(boolean) 3437 * @attr ref android.R.styleable#View_filterTouchesWhenObscured 3438 */ 3439 @ViewDebug.ExportedProperty 3440 public boolean getFilterTouchesWhenObscured() { 3441 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0; 3442 } 3443 3444 /** 3445 * Sets whether the framework should discard touches when the view's 3446 * window is obscured by another visible window. 3447 * Refer to the {@link View} security documentation for more details. 3448 * 3449 * @param enabled True if touch filtering should be enabled. 3450 * 3451 * @see #getFilterTouchesWhenObscured 3452 * @attr ref android.R.styleable#View_filterTouchesWhenObscured 3453 */ 3454 public void setFilterTouchesWhenObscured(boolean enabled) { 3455 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED, 3456 FILTER_TOUCHES_WHEN_OBSCURED); 3457 } 3458 3459 /** 3460 * Returns whether this View is able to take focus. 3461 * 3462 * @return True if this view can take focus, or false otherwise. 3463 * @attr ref android.R.styleable#View_focusable 3464 */ 3465 @ViewDebug.ExportedProperty(category = "focus") 3466 public final boolean isFocusable() { 3467 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK); 3468 } 3469 3470 /** 3471 * When a view is focusable, it may not want to take focus when in touch mode. 3472 * For example, a button would like focus when the user is navigating via a D-pad 3473 * so that the user can click on it, but once the user starts touching the screen, 3474 * the button shouldn't take focus 3475 * @return Whether the view is focusable in touch mode. 3476 * @attr ref android.R.styleable#View_focusableInTouchMode 3477 */ 3478 @ViewDebug.ExportedProperty 3479 public final boolean isFocusableInTouchMode() { 3480 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE); 3481 } 3482 3483 /** 3484 * Find the nearest view in the specified direction that can take focus. 3485 * This does not actually give focus to that view. 3486 * 3487 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT 3488 * 3489 * @return The nearest focusable in the specified direction, or null if none 3490 * can be found. 3491 */ 3492 public View focusSearch(int direction) { 3493 if (mParent != null) { 3494 return mParent.focusSearch(this, direction); 3495 } else { 3496 return null; 3497 } 3498 } 3499 3500 /** 3501 * This method is the last chance for the focused view and its ancestors to 3502 * respond to an arrow key. This is called when the focused view did not 3503 * consume the key internally, nor could the view system find a new view in 3504 * the requested direction to give focus to. 3505 * 3506 * @param focused The currently focused view. 3507 * @param direction The direction focus wants to move. One of FOCUS_UP, 3508 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT. 3509 * @return True if the this view consumed this unhandled move. 3510 */ 3511 public boolean dispatchUnhandledMove(View focused, int direction) { 3512 return false; 3513 } 3514 3515 /** 3516 * If a user manually specified the next view id for a particular direction, 3517 * use the root to look up the view. Once a view is found, it is cached 3518 * for future lookups. 3519 * @param root The root view of the hierarchy containing this view. 3520 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT 3521 * @return The user specified next view, or null if there is none. 3522 */ 3523 View findUserSetNextFocus(View root, int direction) { 3524 switch (direction) { 3525 case FOCUS_LEFT: 3526 if (mNextFocusLeftId == View.NO_ID) return null; 3527 return findViewShouldExist(root, mNextFocusLeftId); 3528 case FOCUS_RIGHT: 3529 if (mNextFocusRightId == View.NO_ID) return null; 3530 return findViewShouldExist(root, mNextFocusRightId); 3531 case FOCUS_UP: 3532 if (mNextFocusUpId == View.NO_ID) return null; 3533 return findViewShouldExist(root, mNextFocusUpId); 3534 case FOCUS_DOWN: 3535 if (mNextFocusDownId == View.NO_ID) return null; 3536 return findViewShouldExist(root, mNextFocusDownId); 3537 } 3538 return null; 3539 } 3540 3541 private static View findViewShouldExist(View root, int childViewId) { 3542 View result = root.findViewById(childViewId); 3543 if (result == null) { 3544 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified " 3545 + "by user for id " + childViewId); 3546 } 3547 return result; 3548 } 3549 3550 /** 3551 * Find and return all focusable views that are descendants of this view, 3552 * possibly including this view if it is focusable itself. 3553 * 3554 * @param direction The direction of the focus 3555 * @return A list of focusable views 3556 */ 3557 public ArrayList<View> getFocusables(int direction) { 3558 ArrayList<View> result = new ArrayList<View>(24); 3559 addFocusables(result, direction); 3560 return result; 3561 } 3562 3563 /** 3564 * Add any focusable views that are descendants of this view (possibly 3565 * including this view if it is focusable itself) to views. If we are in touch mode, 3566 * only add views that are also focusable in touch mode. 3567 * 3568 * @param views Focusable views found so far 3569 * @param direction The direction of the focus 3570 */ 3571 public void addFocusables(ArrayList<View> views, int direction) { 3572 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE); 3573 } 3574 3575 /** 3576 * Adds any focusable views that are descendants of this view (possibly 3577 * including this view if it is focusable itself) to views. This method 3578 * adds all focusable views regardless if we are in touch mode or 3579 * only views focusable in touch mode if we are in touch mode depending on 3580 * the focusable mode paramater. 3581 * 3582 * @param views Focusable views found so far or null if all we are interested is 3583 * the number of focusables. 3584 * @param direction The direction of the focus. 3585 * @param focusableMode The type of focusables to be added. 3586 * 3587 * @see #FOCUSABLES_ALL 3588 * @see #FOCUSABLES_TOUCH_MODE 3589 */ 3590 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { 3591 if (!isFocusable()) { 3592 return; 3593 } 3594 3595 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE && 3596 isInTouchMode() && !isFocusableInTouchMode()) { 3597 return; 3598 } 3599 3600 if (views != null) { 3601 views.add(this); 3602 } 3603 } 3604 3605 /** 3606 * Find and return all touchable views that are descendants of this view, 3607 * possibly including this view if it is touchable itself. 3608 * 3609 * @return A list of touchable views 3610 */ 3611 public ArrayList<View> getTouchables() { 3612 ArrayList<View> result = new ArrayList<View>(); 3613 addTouchables(result); 3614 return result; 3615 } 3616 3617 /** 3618 * Add any touchable views that are descendants of this view (possibly 3619 * including this view if it is touchable itself) to views. 3620 * 3621 * @param views Touchable views found so far 3622 */ 3623 public void addTouchables(ArrayList<View> views) { 3624 final int viewFlags = mViewFlags; 3625 3626 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) 3627 && (viewFlags & ENABLED_MASK) == ENABLED) { 3628 views.add(this); 3629 } 3630 } 3631 3632 /** 3633 * Call this to try to give focus to a specific view or to one of its 3634 * descendants. 3635 * 3636 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false), 3637 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode}) 3638 * while the device is in touch mode. 3639 * 3640 * See also {@link #focusSearch}, which is what you call to say that you 3641 * have focus, and you want your parent to look for the next one. 3642 * 3643 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments 3644 * {@link #FOCUS_DOWN} and <code>null</code>. 3645 * 3646 * @return Whether this view or one of its descendants actually took focus. 3647 */ 3648 public final boolean requestFocus() { 3649 return requestFocus(View.FOCUS_DOWN); 3650 } 3651 3652 3653 /** 3654 * Call this to try to give focus to a specific view or to one of its 3655 * descendants and give it a hint about what direction focus is heading. 3656 * 3657 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false), 3658 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode}) 3659 * while the device is in touch mode. 3660 * 3661 * See also {@link #focusSearch}, which is what you call to say that you 3662 * have focus, and you want your parent to look for the next one. 3663 * 3664 * This is equivalent to calling {@link #requestFocus(int, Rect)} with 3665 * <code>null</code> set for the previously focused rectangle. 3666 * 3667 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT 3668 * @return Whether this view or one of its descendants actually took focus. 3669 */ 3670 public final boolean requestFocus(int direction) { 3671 return requestFocus(direction, null); 3672 } 3673 3674 /** 3675 * Call this to try to give focus to a specific view or to one of its descendants 3676 * and give it hints about the direction and a specific rectangle that the focus 3677 * is coming from. The rectangle can help give larger views a finer grained hint 3678 * about where focus is coming from, and therefore, where to show selection, or 3679 * forward focus change internally. 3680 * 3681 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false), 3682 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode}) 3683 * while the device is in touch mode. 3684 * 3685 * A View will not take focus if it is not visible. 3686 * 3687 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()} 3688 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}. 3689 * 3690 * See also {@link #focusSearch}, which is what you call to say that you 3691 * have focus, and you want your parent to look for the next one. 3692 * 3693 * You may wish to override this method if your custom {@link View} has an internal 3694 * {@link View} that it wishes to forward the request to. 3695 * 3696 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT 3697 * @param previouslyFocusedRect The rectangle (in this View's coordinate system) 3698 * to give a finer grained hint about where focus is coming from. May be null 3699 * if there is no hint. 3700 * @return Whether this view or one of its descendants actually took focus. 3701 */ 3702 public boolean requestFocus(int direction, Rect previouslyFocusedRect) { 3703 // need to be focusable 3704 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE || 3705 (mViewFlags & VISIBILITY_MASK) != VISIBLE) { 3706 return false; 3707 } 3708 3709 // need to be focusable in touch mode if in touch mode 3710 if (isInTouchMode() && 3711 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) { 3712 return false; 3713 } 3714 3715 // need to not have any parents blocking us 3716 if (hasAncestorThatBlocksDescendantFocus()) { 3717 return false; 3718 } 3719 3720 handleFocusGainInternal(direction, previouslyFocusedRect); 3721 return true; 3722 } 3723 3724 /** 3725 * Call this to try to give focus to a specific view or to one of its descendants. This is a 3726 * special variant of {@link #requestFocus() } that will allow views that are not focuable in 3727 * touch mode to request focus when they are touched. 3728 * 3729 * @return Whether this view or one of its descendants actually took focus. 3730 * 3731 * @see #isInTouchMode() 3732 * 3733 */ 3734 public final boolean requestFocusFromTouch() { 3735 // Leave touch mode if we need to 3736 if (isInTouchMode()) { 3737 View root = getRootView(); 3738 if (root != null) { 3739 ViewRoot viewRoot = (ViewRoot)root.getParent(); 3740 if (viewRoot != null) { 3741 viewRoot.ensureTouchMode(false); 3742 } 3743 } 3744 } 3745 return requestFocus(View.FOCUS_DOWN); 3746 } 3747 3748 /** 3749 * @return Whether any ancestor of this view blocks descendant focus. 3750 */ 3751 private boolean hasAncestorThatBlocksDescendantFocus() { 3752 ViewParent ancestor = mParent; 3753 while (ancestor instanceof ViewGroup) { 3754 final ViewGroup vgAncestor = (ViewGroup) ancestor; 3755 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) { 3756 return true; 3757 } else { 3758 ancestor = vgAncestor.getParent(); 3759 } 3760 } 3761 return false; 3762 } 3763 3764 /** 3765 * @hide 3766 */ 3767 public void dispatchStartTemporaryDetach() { 3768 onStartTemporaryDetach(); 3769 } 3770 3771 /** 3772 * This is called when a container is going to temporarily detach a child, with 3773 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}. 3774 * It will either be followed by {@link #onFinishTemporaryDetach()} or 3775 * {@link #onDetachedFromWindow()} when the container is done. 3776 */ 3777 public void onStartTemporaryDetach() { 3778 removeUnsetPressCallback(); 3779 mPrivateFlags |= CANCEL_NEXT_UP_EVENT; 3780 } 3781 3782 /** 3783 * @hide 3784 */ 3785 public void dispatchFinishTemporaryDetach() { 3786 onFinishTemporaryDetach(); 3787 } 3788 3789 /** 3790 * Called after {@link #onStartTemporaryDetach} when the container is done 3791 * changing the view. 3792 */ 3793 public void onFinishTemporaryDetach() { 3794 } 3795 3796 /** 3797 * capture information of this view for later analysis: developement only 3798 * check dynamic switch to make sure we only dump view 3799 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set 3800 */ 3801 private static void captureViewInfo(String subTag, View v) { 3802 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) { 3803 return; 3804 } 3805 ViewDebug.dumpCapturedView(subTag, v); 3806 } 3807 3808 /** 3809 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState} 3810 * for this view's window. Returns null if the view is not currently attached 3811 * to the window. Normally you will not need to use this directly, but 3812 * just use the standard high-level event callbacks like {@link #onKeyDown}. 3813 */ 3814 public KeyEvent.DispatcherState getKeyDispatcherState() { 3815 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null; 3816 } 3817 3818 /** 3819 * Dispatch a key event before it is processed by any input method 3820 * associated with the view hierarchy. This can be used to intercept 3821 * key events in special situations before the IME consumes them; a 3822 * typical example would be handling the BACK key to update the application's 3823 * UI instead of allowing the IME to see it and close itself. 3824 * 3825 * @param event The key event to be dispatched. 3826 * @return True if the event was handled, false otherwise. 3827 */ 3828 public boolean dispatchKeyEventPreIme(KeyEvent event) { 3829 return onKeyPreIme(event.getKeyCode(), event); 3830 } 3831 3832 /** 3833 * Dispatch a key event to the next view on the focus path. This path runs 3834 * from the top of the view tree down to the currently focused view. If this 3835 * view has focus, it will dispatch to itself. Otherwise it will dispatch 3836 * the next node down the focus path. This method also fires any key 3837 * listeners. 3838 * 3839 * @param event The key event to be dispatched. 3840 * @return True if the event was handled, false otherwise. 3841 */ 3842 public boolean dispatchKeyEvent(KeyEvent event) { 3843 // If any attached key listener a first crack at the event. 3844 //noinspection SimplifiableIfStatement 3845 3846 if (android.util.Config.LOGV) { 3847 captureViewInfo("captureViewKeyEvent", this); 3848 } 3849 3850 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED 3851 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) { 3852 return true; 3853 } 3854 3855 return event.dispatch(this, mAttachInfo != null 3856 ? mAttachInfo.mKeyDispatchState : null, this); 3857 } 3858 3859 /** 3860 * Dispatches a key shortcut event. 3861 * 3862 * @param event The key event to be dispatched. 3863 * @return True if the event was handled by the view, false otherwise. 3864 */ 3865 public boolean dispatchKeyShortcutEvent(KeyEvent event) { 3866 return onKeyShortcut(event.getKeyCode(), event); 3867 } 3868 3869 /** 3870 * Pass the touch screen motion event down to the target view, or this 3871 * view if it is the target. 3872 * 3873 * @param event The motion event to be dispatched. 3874 * @return True if the event was handled by the view, false otherwise. 3875 */ 3876 public boolean dispatchTouchEvent(MotionEvent event) { 3877 if (!onFilterTouchEventForSecurity(event)) { 3878 return false; 3879 } 3880 3881 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && 3882 mOnTouchListener.onTouch(this, event)) { 3883 return true; 3884 } 3885 return onTouchEvent(event); 3886 } 3887 3888 /** 3889 * Filter the touch event to apply security policies. 3890 * 3891 * @param event The motion event to be filtered. 3892 * @return True if the event should be dispatched, false if the event should be dropped. 3893 * 3894 * @see #getFilterTouchesWhenObscured 3895 */ 3896 public boolean onFilterTouchEventForSecurity(MotionEvent event) { 3897 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0 3898 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) { 3899 // Window is obscured, drop this touch. 3900 return false; 3901 } 3902 return true; 3903 } 3904 3905 /** 3906 * Pass a trackball motion event down to the focused view. 3907 * 3908 * @param event The motion event to be dispatched. 3909 * @return True if the event was handled by the view, false otherwise. 3910 */ 3911 public boolean dispatchTrackballEvent(MotionEvent event) { 3912 //Log.i("view", "view=" + this + ", " + event.toString()); 3913 return onTrackballEvent(event); 3914 } 3915 3916 /** 3917 * Called when the window containing this view gains or loses window focus. 3918 * ViewGroups should override to route to their children. 3919 * 3920 * @param hasFocus True if the window containing this view now has focus, 3921 * false otherwise. 3922 */ 3923 public void dispatchWindowFocusChanged(boolean hasFocus) { 3924 onWindowFocusChanged(hasFocus); 3925 } 3926 3927 /** 3928 * Called when the window containing this view gains or loses focus. Note 3929 * that this is separate from view focus: to receive key events, both 3930 * your view and its window must have focus. If a window is displayed 3931 * on top of yours that takes input focus, then your own window will lose 3932 * focus but the view focus will remain unchanged. 3933 * 3934 * @param hasWindowFocus True if the window containing this view now has 3935 * focus, false otherwise. 3936 */ 3937 public void onWindowFocusChanged(boolean hasWindowFocus) { 3938 InputMethodManager imm = InputMethodManager.peekInstance(); 3939 if (!hasWindowFocus) { 3940 if (isPressed()) { 3941 setPressed(false); 3942 } 3943 if (imm != null && (mPrivateFlags & FOCUSED) != 0) { 3944 imm.focusOut(this); 3945 } 3946 removeLongPressCallback(); 3947 onFocusLost(); 3948 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) { 3949 imm.focusIn(this); 3950 } 3951 refreshDrawableState(); 3952 } 3953 3954 /** 3955 * Returns true if this view is in a window that currently has window focus. 3956 * Note that this is not the same as the view itself having focus. 3957 * 3958 * @return True if this view is in a window that currently has window focus. 3959 */ 3960 public boolean hasWindowFocus() { 3961 return mAttachInfo != null && mAttachInfo.mHasWindowFocus; 3962 } 3963 3964 /** 3965 * Dispatch a view visibility change down the view hierarchy. 3966 * ViewGroups should override to route to their children. 3967 * @param changedView The view whose visibility changed. Could be 'this' or 3968 * an ancestor view. 3969 * @param visibility The new visibility of changedView: {@link #VISIBLE}, 3970 * {@link #INVISIBLE} or {@link #GONE}. 3971 */ 3972 protected void dispatchVisibilityChanged(View changedView, int visibility) { 3973 onVisibilityChanged(changedView, visibility); 3974 } 3975 3976 /** 3977 * Called when the visibility of the view or an ancestor of the view is changed. 3978 * @param changedView The view whose visibility changed. Could be 'this' or 3979 * an ancestor view. 3980 * @param visibility The new visibility of changedView: {@link #VISIBLE}, 3981 * {@link #INVISIBLE} or {@link #GONE}. 3982 */ 3983 protected void onVisibilityChanged(View changedView, int visibility) { 3984 if (visibility == VISIBLE) { 3985 if (mAttachInfo != null) { 3986 initialAwakenScrollBars(); 3987 } else { 3988 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH; 3989 } 3990 } 3991 } 3992 3993 /** 3994 * Dispatch a hint about whether this view is displayed. For instance, when 3995 * a View moves out of the screen, it might receives a display hint indicating 3996 * the view is not displayed. Applications should not <em>rely</em> on this hint 3997 * as there is no guarantee that they will receive one. 3998 * 3999 * @param hint A hint about whether or not this view is displayed: 4000 * {@link #VISIBLE} or {@link #INVISIBLE}. 4001 */ 4002 public void dispatchDisplayHint(int hint) { 4003 onDisplayHint(hint); 4004 } 4005 4006 /** 4007 * Gives this view a hint about whether is displayed or not. For instance, when 4008 * a View moves out of the screen, it might receives a display hint indicating 4009 * the view is not displayed. Applications should not <em>rely</em> on this hint 4010 * as there is no guarantee that they will receive one. 4011 * 4012 * @param hint A hint about whether or not this view is displayed: 4013 * {@link #VISIBLE} or {@link #INVISIBLE}. 4014 */ 4015 protected void onDisplayHint(int hint) { 4016 } 4017 4018 /** 4019 * Dispatch a window visibility change down the view hierarchy. 4020 * ViewGroups should override to route to their children. 4021 * 4022 * @param visibility The new visibility of the window. 4023 * 4024 * @see #onWindowVisibilityChanged 4025 */ 4026 public void dispatchWindowVisibilityChanged(int visibility) { 4027 onWindowVisibilityChanged(visibility); 4028 } 4029 4030 /** 4031 * Called when the window containing has change its visibility 4032 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note 4033 * that this tells you whether or not your window is being made visible 4034 * to the window manager; this does <em>not</em> tell you whether or not 4035 * your window is obscured by other windows on the screen, even if it 4036 * is itself visible. 4037 * 4038 * @param visibility The new visibility of the window. 4039 */ 4040 protected void onWindowVisibilityChanged(int visibility) { 4041 if (visibility == VISIBLE) { 4042 initialAwakenScrollBars(); 4043 } 4044 } 4045 4046 /** 4047 * Returns the current visibility of the window this view is attached to 4048 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}). 4049 * 4050 * @return Returns the current visibility of the view's window. 4051 */ 4052 public int getWindowVisibility() { 4053 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE; 4054 } 4055 4056 /** 4057 * Retrieve the overall visible display size in which the window this view is 4058 * attached to has been positioned in. This takes into account screen 4059 * decorations above the window, for both cases where the window itself 4060 * is being position inside of them or the window is being placed under 4061 * then and covered insets are used for the window to position its content 4062 * inside. In effect, this tells you the available area where content can 4063 * be placed and remain visible to users. 4064 * 4065 * <p>This function requires an IPC back to the window manager to retrieve 4066 * the requested information, so should not be used in performance critical 4067 * code like drawing. 4068 * 4069 * @param outRect Filled in with the visible display frame. If the view 4070 * is not attached to a window, this is simply the raw display size. 4071 */ 4072 public void getWindowVisibleDisplayFrame(Rect outRect) { 4073 if (mAttachInfo != null) { 4074 try { 4075 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect); 4076 } catch (RemoteException e) { 4077 return; 4078 } 4079 // XXX This is really broken, and probably all needs to be done 4080 // in the window manager, and we need to know more about whether 4081 // we want the area behind or in front of the IME. 4082 final Rect insets = mAttachInfo.mVisibleInsets; 4083 outRect.left += insets.left; 4084 outRect.top += insets.top; 4085 outRect.right -= insets.right; 4086 outRect.bottom -= insets.bottom; 4087 return; 4088 } 4089 Display d = WindowManagerImpl.getDefault().getDefaultDisplay(); 4090 outRect.set(0, 0, d.getWidth(), d.getHeight()); 4091 } 4092 4093 /** 4094 * Dispatch a notification about a resource configuration change down 4095 * the view hierarchy. 4096 * ViewGroups should override to route to their children. 4097 * 4098 * @param newConfig The new resource configuration. 4099 * 4100 * @see #onConfigurationChanged 4101 */ 4102 public void dispatchConfigurationChanged(Configuration newConfig) { 4103 onConfigurationChanged(newConfig); 4104 } 4105 4106 /** 4107 * Called when the current configuration of the resources being used 4108 * by the application have changed. You can use this to decide when 4109 * to reload resources that can changed based on orientation and other 4110 * configuration characterstics. You only need to use this if you are 4111 * not relying on the normal {@link android.app.Activity} mechanism of 4112 * recreating the activity instance upon a configuration change. 4113 * 4114 * @param newConfig The new resource configuration. 4115 */ 4116 protected void onConfigurationChanged(Configuration newConfig) { 4117 } 4118 4119 /** 4120 * Private function to aggregate all per-view attributes in to the view 4121 * root. 4122 */ 4123 void dispatchCollectViewAttributes(int visibility) { 4124 performCollectViewAttributes(visibility); 4125 } 4126 4127 void performCollectViewAttributes(int visibility) { 4128 //noinspection PointlessBitwiseExpression 4129 if (((visibility | mViewFlags) & (VISIBILITY_MASK | KEEP_SCREEN_ON)) 4130 == (VISIBLE | KEEP_SCREEN_ON)) { 4131 mAttachInfo.mKeepScreenOn = true; 4132 } 4133 } 4134 4135 void needGlobalAttributesUpdate(boolean force) { 4136 AttachInfo ai = mAttachInfo; 4137 if (ai != null) { 4138 if (ai.mKeepScreenOn || force) { 4139 ai.mRecomputeGlobalAttributes = true; 4140 } 4141 } 4142 } 4143 4144 /** 4145 * Returns whether the device is currently in touch mode. Touch mode is entered 4146 * once the user begins interacting with the device by touch, and affects various 4147 * things like whether focus is always visible to the user. 4148 * 4149 * @return Whether the device is in touch mode. 4150 */ 4151 @ViewDebug.ExportedProperty 4152 public boolean isInTouchMode() { 4153 if (mAttachInfo != null) { 4154 return mAttachInfo.mInTouchMode; 4155 } else { 4156 return ViewRoot.isInTouchMode(); 4157 } 4158 } 4159 4160 /** 4161 * Returns the context the view is running in, through which it can 4162 * access the current theme, resources, etc. 4163 * 4164 * @return The view's Context. 4165 */ 4166 @ViewDebug.CapturedViewProperty 4167 public final Context getContext() { 4168 return mContext; 4169 } 4170 4171 /** 4172 * Handle a key event before it is processed by any input method 4173 * associated with the view hierarchy. This can be used to intercept 4174 * key events in special situations before the IME consumes them; a 4175 * typical example would be handling the BACK key to update the application's 4176 * UI instead of allowing the IME to see it and close itself. 4177 * 4178 * @param keyCode The value in event.getKeyCode(). 4179 * @param event Description of the key event. 4180 * @return If you handled the event, return true. If you want to allow the 4181 * event to be handled by the next receiver, return false. 4182 */ 4183 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 4184 return false; 4185 } 4186 4187 /** 4188 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) 4189 * KeyEvent.Callback.onKeyMultiple()}: perform press of the view 4190 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER} 4191 * is released, if the view is enabled and clickable. 4192 * 4193 * @param keyCode A key code that represents the button pressed, from 4194 * {@link android.view.KeyEvent}. 4195 * @param event The KeyEvent object that defines the button action. 4196 */ 4197 public boolean onKeyDown(int keyCode, KeyEvent event) { 4198 boolean result = false; 4199 4200 switch (keyCode) { 4201 case KeyEvent.KEYCODE_DPAD_CENTER: 4202 case KeyEvent.KEYCODE_ENTER: { 4203 if ((mViewFlags & ENABLED_MASK) == DISABLED) { 4204 return true; 4205 } 4206 // Long clickable items don't necessarily have to be clickable 4207 if (((mViewFlags & CLICKABLE) == CLICKABLE || 4208 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) && 4209 (event.getRepeatCount() == 0)) { 4210 setPressed(true); 4211 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) { 4212 postCheckForLongClick(0); 4213 } 4214 return true; 4215 } 4216 break; 4217 } 4218 } 4219 return result; 4220 } 4221 4222 /** 4223 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) 4224 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle 4225 * the event). 4226 */ 4227 public boolean onKeyLongPress(int keyCode, KeyEvent event) { 4228 return false; 4229 } 4230 4231 /** 4232 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) 4233 * KeyEvent.Callback.onKeyMultiple()}: perform clicking of the view 4234 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or 4235 * {@link KeyEvent#KEYCODE_ENTER} is released. 4236 * 4237 * @param keyCode A key code that represents the button pressed, from 4238 * {@link android.view.KeyEvent}. 4239 * @param event The KeyEvent object that defines the button action. 4240 */ 4241 public boolean onKeyUp(int keyCode, KeyEvent event) { 4242 boolean result = false; 4243 4244 switch (keyCode) { 4245 case KeyEvent.KEYCODE_DPAD_CENTER: 4246 case KeyEvent.KEYCODE_ENTER: { 4247 if ((mViewFlags & ENABLED_MASK) == DISABLED) { 4248 return true; 4249 } 4250 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) { 4251 setPressed(false); 4252 4253 if (!mHasPerformedLongPress) { 4254 // This is a tap, so remove the longpress check 4255 removeLongPressCallback(); 4256 4257 result = performClick(); 4258 } 4259 } 4260 break; 4261 } 4262 } 4263 return result; 4264 } 4265 4266 /** 4267 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) 4268 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle 4269 * the event). 4270 * 4271 * @param keyCode A key code that represents the button pressed, from 4272 * {@link android.view.KeyEvent}. 4273 * @param repeatCount The number of times the action was made. 4274 * @param event The KeyEvent object that defines the button action. 4275 */ 4276 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { 4277 return false; 4278 } 4279 4280 /** 4281 * Called when an unhandled key shortcut event occurs. 4282 * 4283 * @param keyCode The value in event.getKeyCode(). 4284 * @param event Description of the key event. 4285 * @return If you handled the event, return true. If you want to allow the 4286 * event to be handled by the next receiver, return false. 4287 */ 4288 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 4289 return false; 4290 } 4291 4292 /** 4293 * Check whether the called view is a text editor, in which case it 4294 * would make sense to automatically display a soft input window for 4295 * it. Subclasses should override this if they implement 4296 * {@link #onCreateInputConnection(EditorInfo)} to return true if 4297 * a call on that method would return a non-null InputConnection, and 4298 * they are really a first-class editor that the user would normally 4299 * start typing on when the go into a window containing your view. 4300 * 4301 * <p>The default implementation always returns false. This does 4302 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)} 4303 * will not be called or the user can not otherwise perform edits on your 4304 * view; it is just a hint to the system that this is not the primary 4305 * purpose of this view. 4306 * 4307 * @return Returns true if this view is a text editor, else false. 4308 */ 4309 public boolean onCheckIsTextEditor() { 4310 return false; 4311 } 4312 4313 /** 4314 * Create a new InputConnection for an InputMethod to interact 4315 * with the view. The default implementation returns null, since it doesn't 4316 * support input methods. You can override this to implement such support. 4317 * This is only needed for views that take focus and text input. 4318 * 4319 * <p>When implementing this, you probably also want to implement 4320 * {@link #onCheckIsTextEditor()} to indicate you will return a 4321 * non-null InputConnection. 4322 * 4323 * @param outAttrs Fill in with attribute information about the connection. 4324 */ 4325 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 4326 return null; 4327 } 4328 4329 /** 4330 * Called by the {@link android.view.inputmethod.InputMethodManager} 4331 * when a view who is not the current 4332 * input connection target is trying to make a call on the manager. The 4333 * default implementation returns false; you can override this to return 4334 * true for certain views if you are performing InputConnection proxying 4335 * to them. 4336 * @param view The View that is making the InputMethodManager call. 4337 * @return Return true to allow the call, false to reject. 4338 */ 4339 public boolean checkInputConnectionProxy(View view) { 4340 return false; 4341 } 4342 4343 /** 4344 * Show the context menu for this view. It is not safe to hold on to the 4345 * menu after returning from this method. 4346 * 4347 * You should normally not overload this method. Overload 4348 * {@link #onCreateContextMenu(ContextMenu)} or define an 4349 * {@link OnCreateContextMenuListener} to add items to the context menu. 4350 * 4351 * @param menu The context menu to populate 4352 */ 4353 public void createContextMenu(ContextMenu menu) { 4354 ContextMenuInfo menuInfo = getContextMenuInfo(); 4355 4356 // Sets the current menu info so all items added to menu will have 4357 // my extra info set. 4358 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo); 4359 4360 onCreateContextMenu(menu); 4361 if (mOnCreateContextMenuListener != null) { 4362 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo); 4363 } 4364 4365 // Clear the extra information so subsequent items that aren't mine don't 4366 // have my extra info. 4367 ((MenuBuilder)menu).setCurrentMenuInfo(null); 4368 4369 if (mParent != null) { 4370 mParent.createContextMenu(menu); 4371 } 4372 } 4373 4374 /** 4375 * Views should implement this if they have extra information to associate 4376 * with the context menu. The return result is supplied as a parameter to 4377 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} 4378 * callback. 4379 * 4380 * @return Extra information about the item for which the context menu 4381 * should be shown. This information will vary across different 4382 * subclasses of View. 4383 */ 4384 protected ContextMenuInfo getContextMenuInfo() { 4385 return null; 4386 } 4387 4388 /** 4389 * Views should implement this if the view itself is going to add items to 4390 * the context menu. 4391 * 4392 * @param menu the context menu to populate 4393 */ 4394 protected void onCreateContextMenu(ContextMenu menu) { 4395 } 4396 4397 /** 4398 * Implement this method to handle trackball motion events. The 4399 * <em>relative</em> movement of the trackball since the last event 4400 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and 4401 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so 4402 * that a movement of 1 corresponds to the user pressing one DPAD key (so 4403 * they will often be fractional values, representing the more fine-grained 4404 * movement information available from a trackball). 4405 * 4406 * @param event The motion event. 4407 * @return True if the event was handled, false otherwise. 4408 */ 4409 public boolean onTrackballEvent(MotionEvent event) { 4410 return false; 4411 } 4412 4413 /** 4414 * Implement this method to handle touch screen motion events. 4415 * 4416 * @param event The motion event. 4417 * @return True if the event was handled, false otherwise. 4418 */ 4419 public boolean onTouchEvent(MotionEvent event) { 4420 final int viewFlags = mViewFlags; 4421 4422 if ((viewFlags & ENABLED_MASK) == DISABLED) { 4423 // A disabled view that is clickable still consumes the touch 4424 // events, it just doesn't respond to them. 4425 return (((viewFlags & CLICKABLE) == CLICKABLE || 4426 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)); 4427 } 4428 4429 if (mTouchDelegate != null) { 4430 if (mTouchDelegate.onTouchEvent(event)) { 4431 return true; 4432 } 4433 } 4434 4435 if (((viewFlags & CLICKABLE) == CLICKABLE || 4436 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) { 4437 switch (event.getAction()) { 4438 case MotionEvent.ACTION_UP: 4439 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0; 4440 if ((mPrivateFlags & PRESSED) != 0 || prepressed) { 4441 // take focus if we don't have it already and we should in 4442 // touch mode. 4443 boolean focusTaken = false; 4444 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { 4445 focusTaken = requestFocus(); 4446 } 4447 4448 if (!mHasPerformedLongPress) { 4449 // This is a tap, so remove the longpress check 4450 removeLongPressCallback(); 4451 4452 // Only perform take click actions if we were in the pressed state 4453 if (!focusTaken) { 4454 // Use a Runnable and post this rather than calling 4455 // performClick directly. This lets other visual state 4456 // of the view update before click actions start. 4457 if (mPerformClick == null) { 4458 mPerformClick = new PerformClick(); 4459 } 4460 if (!post(mPerformClick)) { 4461 performClick(); 4462 } 4463 } 4464 } 4465 4466 if (mUnsetPressedState == null) { 4467 mUnsetPressedState = new UnsetPressedState(); 4468 } 4469 4470 if (prepressed) { 4471 mPrivateFlags |= PRESSED; 4472 refreshDrawableState(); 4473 postDelayed(mUnsetPressedState, 4474 ViewConfiguration.getPressedStateDuration()); 4475 } else if (!post(mUnsetPressedState)) { 4476 // If the post failed, unpress right now 4477 mUnsetPressedState.run(); 4478 } 4479 removeTapCallback(); 4480 } 4481 break; 4482 4483 case MotionEvent.ACTION_DOWN: 4484 if (mPendingCheckForTap == null) { 4485 mPendingCheckForTap = new CheckForTap(); 4486 } 4487 mPrivateFlags |= PREPRESSED; 4488 mHasPerformedLongPress = false; 4489 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); 4490 break; 4491 4492 case MotionEvent.ACTION_CANCEL: 4493 mPrivateFlags &= ~PRESSED; 4494 refreshDrawableState(); 4495 removeTapCallback(); 4496 break; 4497 4498 case MotionEvent.ACTION_MOVE: 4499 final int x = (int) event.getX(); 4500 final int y = (int) event.getY(); 4501 4502 // Be lenient about moving outside of buttons 4503 int slop = mTouchSlop; 4504 if ((x < 0 - slop) || (x >= getWidth() + slop) || 4505 (y < 0 - slop) || (y >= getHeight() + slop)) { 4506 // Outside button 4507 removeTapCallback(); 4508 if ((mPrivateFlags & PRESSED) != 0) { 4509 // Remove any future long press/tap checks 4510 removeLongPressCallback(); 4511 4512 // Need to switch from pressed to not pressed 4513 mPrivateFlags &= ~PRESSED; 4514 refreshDrawableState(); 4515 } 4516 } 4517 break; 4518 } 4519 return true; 4520 } 4521 4522 return false; 4523 } 4524 4525 /** 4526 * Remove the longpress detection timer. 4527 */ 4528 private void removeLongPressCallback() { 4529 if (mPendingCheckForLongPress != null) { 4530 removeCallbacks(mPendingCheckForLongPress); 4531 } 4532 } 4533 4534 /** 4535 * Remove the prepress detection timer. 4536 */ 4537 private void removeUnsetPressCallback() { 4538 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) { 4539 setPressed(false); 4540 removeCallbacks(mUnsetPressedState); 4541 } 4542 } 4543 4544 /** 4545 * Remove the tap detection timer. 4546 */ 4547 private void removeTapCallback() { 4548 if (mPendingCheckForTap != null) { 4549 mPrivateFlags &= ~PREPRESSED; 4550 removeCallbacks(mPendingCheckForTap); 4551 } 4552 } 4553 4554 /** 4555 * Cancels a pending long press. Your subclass can use this if you 4556 * want the context menu to come up if the user presses and holds 4557 * at the same place, but you don't want it to come up if they press 4558 * and then move around enough to cause scrolling. 4559 */ 4560 public void cancelLongPress() { 4561 removeLongPressCallback(); 4562 4563 /* 4564 * The prepressed state handled by the tap callback is a display 4565 * construct, but the tap callback will post a long press callback 4566 * less its own timeout. Remove it here. 4567 */ 4568 removeTapCallback(); 4569 } 4570 4571 /** 4572 * Sets the TouchDelegate for this View. 4573 */ 4574 public void setTouchDelegate(TouchDelegate delegate) { 4575 mTouchDelegate = delegate; 4576 } 4577 4578 /** 4579 * Gets the TouchDelegate for this View. 4580 */ 4581 public TouchDelegate getTouchDelegate() { 4582 return mTouchDelegate; 4583 } 4584 4585 /** 4586 * Set flags controlling behavior of this view. 4587 * 4588 * @param flags Constant indicating the value which should be set 4589 * @param mask Constant indicating the bit range that should be changed 4590 */ 4591 void setFlags(int flags, int mask) { 4592 int old = mViewFlags; 4593 mViewFlags = (mViewFlags & ~mask) | (flags & mask); 4594 4595 int changed = mViewFlags ^ old; 4596 if (changed == 0) { 4597 return; 4598 } 4599 int privateFlags = mPrivateFlags; 4600 4601 /* Check if the FOCUSABLE bit has changed */ 4602 if (((changed & FOCUSABLE_MASK) != 0) && 4603 ((privateFlags & HAS_BOUNDS) !=0)) { 4604 if (((old & FOCUSABLE_MASK) == FOCUSABLE) 4605 && ((privateFlags & FOCUSED) != 0)) { 4606 /* Give up focus if we are no longer focusable */ 4607 clearFocus(); 4608 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE) 4609 && ((privateFlags & FOCUSED) == 0)) { 4610 /* 4611 * Tell the view system that we are now available to take focus 4612 * if no one else already has it. 4613 */ 4614 if (mParent != null) mParent.focusableViewAvailable(this); 4615 } 4616 } 4617 4618 if ((flags & VISIBILITY_MASK) == VISIBLE) { 4619 if ((changed & VISIBILITY_MASK) != 0) { 4620 /* 4621 * If this view is becoming visible, set the DRAWN flag so that 4622 * the next invalidate() will not be skipped. 4623 */ 4624 mPrivateFlags |= DRAWN; 4625 4626 needGlobalAttributesUpdate(true); 4627 4628 // a view becoming visible is worth notifying the parent 4629 // about in case nothing has focus. even if this specific view 4630 // isn't focusable, it may contain something that is, so let 4631 // the root view try to give this focus if nothing else does. 4632 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) { 4633 mParent.focusableViewAvailable(this); 4634 } 4635 } 4636 } 4637 4638 /* Check if the GONE bit has changed */ 4639 if ((changed & GONE) != 0) { 4640 needGlobalAttributesUpdate(false); 4641 requestLayout(); 4642 invalidate(); 4643 4644 if (((mViewFlags & VISIBILITY_MASK) == GONE)) { 4645 if (hasFocus()) clearFocus(); 4646 destroyDrawingCache(); 4647 } 4648 if (mAttachInfo != null) { 4649 mAttachInfo.mViewVisibilityChanged = true; 4650 } 4651 } 4652 4653 /* Check if the VISIBLE bit has changed */ 4654 if ((changed & INVISIBLE) != 0) { 4655 needGlobalAttributesUpdate(false); 4656 invalidate(); 4657 4658 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) { 4659 // root view becoming invisible shouldn't clear focus 4660 if (getRootView() != this) { 4661 clearFocus(); 4662 } 4663 } 4664 if (mAttachInfo != null) { 4665 mAttachInfo.mViewVisibilityChanged = true; 4666 } 4667 } 4668 4669 if ((changed & VISIBILITY_MASK) != 0) { 4670 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK)); 4671 } 4672 4673 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) { 4674 destroyDrawingCache(); 4675 } 4676 4677 if ((changed & DRAWING_CACHE_ENABLED) != 0) { 4678 destroyDrawingCache(); 4679 mPrivateFlags &= ~DRAWING_CACHE_VALID; 4680 } 4681 4682 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) { 4683 destroyDrawingCache(); 4684 mPrivateFlags &= ~DRAWING_CACHE_VALID; 4685 } 4686 4687 if ((changed & DRAW_MASK) != 0) { 4688 if ((mViewFlags & WILL_NOT_DRAW) != 0) { 4689 if (mBGDrawable != null) { 4690 mPrivateFlags &= ~SKIP_DRAW; 4691 mPrivateFlags |= ONLY_DRAWS_BACKGROUND; 4692 } else { 4693 mPrivateFlags |= SKIP_DRAW; 4694 } 4695 } else { 4696 mPrivateFlags &= ~SKIP_DRAW; 4697 } 4698 requestLayout(); 4699 invalidate(); 4700 } 4701 4702 if ((changed & KEEP_SCREEN_ON) != 0) { 4703 if (mParent != null) { 4704 mParent.recomputeViewAttributes(this); 4705 } 4706 } 4707 } 4708 4709 /** 4710 * Change the view's z order in the tree, so it's on top of other sibling 4711 * views 4712 */ 4713 public void bringToFront() { 4714 if (mParent != null) { 4715 mParent.bringChildToFront(this); 4716 } 4717 } 4718 4719 /** 4720 * This is called in response to an internal scroll in this view (i.e., the 4721 * view scrolled its own contents). This is typically as a result of 4722 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been 4723 * called. 4724 * 4725 * @param l Current horizontal scroll origin. 4726 * @param t Current vertical scroll origin. 4727 * @param oldl Previous horizontal scroll origin. 4728 * @param oldt Previous vertical scroll origin. 4729 */ 4730 protected void onScrollChanged(int l, int t, int oldl, int oldt) { 4731 mBackgroundSizeChanged = true; 4732 4733 final AttachInfo ai = mAttachInfo; 4734 if (ai != null) { 4735 ai.mViewScrollChanged = true; 4736 } 4737 } 4738 4739 /** 4740 * This is called during layout when the size of this view has changed. If 4741 * you were just added to the view hierarchy, you're called with the old 4742 * values of 0. 4743 * 4744 * @param w Current width of this view. 4745 * @param h Current height of this view. 4746 * @param oldw Old width of this view. 4747 * @param oldh Old height of this view. 4748 */ 4749 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 4750 } 4751 4752 /** 4753 * Called by draw to draw the child views. This may be overridden 4754 * by derived classes to gain control just before its children are drawn 4755 * (but after its own view has been drawn). 4756 * @param canvas the canvas on which to draw the view 4757 */ 4758 protected void dispatchDraw(Canvas canvas) { 4759 } 4760 4761 /** 4762 * Gets the parent of this view. Note that the parent is a 4763 * ViewParent and not necessarily a View. 4764 * 4765 * @return Parent of this view. 4766 */ 4767 public final ViewParent getParent() { 4768 return mParent; 4769 } 4770 4771 /** 4772 * Return the scrolled left position of this view. This is the left edge of 4773 * the displayed part of your view. You do not need to draw any pixels 4774 * farther left, since those are outside of the frame of your view on 4775 * screen. 4776 * 4777 * @return The left edge of the displayed part of your view, in pixels. 4778 */ 4779 public final int getScrollX() { 4780 return mScrollX; 4781 } 4782 4783 /** 4784 * Return the scrolled top position of this view. This is the top edge of 4785 * the displayed part of your view. You do not need to draw any pixels above 4786 * it, since those are outside of the frame of your view on screen. 4787 * 4788 * @return The top edge of the displayed part of your view, in pixels. 4789 */ 4790 public final int getScrollY() { 4791 return mScrollY; 4792 } 4793 4794 /** 4795 * Return the width of the your view. 4796 * 4797 * @return The width of your view, in pixels. 4798 */ 4799 @ViewDebug.ExportedProperty(category = "layout") 4800 public final int getWidth() { 4801 return mRight - mLeft; 4802 } 4803 4804 /** 4805 * Return the height of your view. 4806 * 4807 * @return The height of your view, in pixels. 4808 */ 4809 @ViewDebug.ExportedProperty(category = "layout") 4810 public final int getHeight() { 4811 return mBottom - mTop; 4812 } 4813 4814 /** 4815 * Return the visible drawing bounds of your view. Fills in the output 4816 * rectangle with the values from getScrollX(), getScrollY(), 4817 * getWidth(), and getHeight(). 4818 * 4819 * @param outRect The (scrolled) drawing bounds of the view. 4820 */ 4821 public void getDrawingRect(Rect outRect) { 4822 outRect.left = mScrollX; 4823 outRect.top = mScrollY; 4824 outRect.right = mScrollX + (mRight - mLeft); 4825 outRect.bottom = mScrollY + (mBottom - mTop); 4826 } 4827 4828 /** 4829 * The width of this view as measured in the most recent call to measure(). 4830 * This should be used during measurement and layout calculations only. Use 4831 * {@link #getWidth()} to see how wide a view is after layout. 4832 * 4833 * @return The measured width of this view. 4834 */ 4835 public final int getMeasuredWidth() { 4836 return mMeasuredWidth; 4837 } 4838 4839 /** 4840 * The height of this view as measured in the most recent call to measure(). 4841 * This should be used during measurement and layout calculations only. Use 4842 * {@link #getHeight()} to see how tall a view is after layout. 4843 * 4844 * @return The measured height of this view. 4845 */ 4846 public final int getMeasuredHeight() { 4847 return mMeasuredHeight; 4848 } 4849 4850 /** 4851 * Top position of this view relative to its parent. 4852 * 4853 * @return The top of this view, in pixels. 4854 */ 4855 @ViewDebug.CapturedViewProperty 4856 public final int getTop() { 4857 return mTop; 4858 } 4859 4860 /** 4861 * Bottom position of this view relative to its parent. 4862 * 4863 * @return The bottom of this view, in pixels. 4864 */ 4865 @ViewDebug.CapturedViewProperty 4866 public final int getBottom() { 4867 return mBottom; 4868 } 4869 4870 /** 4871 * Left position of this view relative to its parent. 4872 * 4873 * @return The left edge of this view, in pixels. 4874 */ 4875 @ViewDebug.CapturedViewProperty 4876 public final int getLeft() { 4877 return mLeft; 4878 } 4879 4880 /** 4881 * Right position of this view relative to its parent. 4882 * 4883 * @return The right edge of this view, in pixels. 4884 */ 4885 @ViewDebug.CapturedViewProperty 4886 public final int getRight() { 4887 return mRight; 4888 } 4889 4890 /** 4891 * Hit rectangle in parent's coordinates 4892 * 4893 * @param outRect The hit rectangle of the view. 4894 */ 4895 public void getHitRect(Rect outRect) { 4896 outRect.set(mLeft, mTop, mRight, mBottom); 4897 } 4898 4899 /** 4900 * When a view has focus and the user navigates away from it, the next view is searched for 4901 * starting from the rectangle filled in by this method. 4902 * 4903 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your 4904 * view maintains some idea of internal selection, such as a cursor, or a selected row 4905 * or column, you should override this method and fill in a more specific rectangle. 4906 * 4907 * @param r The rectangle to fill in, in this view's coordinates. 4908 */ 4909 public void getFocusedRect(Rect r) { 4910 getDrawingRect(r); 4911 } 4912 4913 /** 4914 * If some part of this view is not clipped by any of its parents, then 4915 * return that area in r in global (root) coordinates. To convert r to local 4916 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x, 4917 * -globalOffset.y)) If the view is completely clipped or translated out, 4918 * return false. 4919 * 4920 * @param r If true is returned, r holds the global coordinates of the 4921 * visible portion of this view. 4922 * @param globalOffset If true is returned, globalOffset holds the dx,dy 4923 * between this view and its root. globalOffet may be null. 4924 * @return true if r is non-empty (i.e. part of the view is visible at the 4925 * root level. 4926 */ 4927 public boolean