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 android.app.Application; 20 import android.content.Context; 21 import android.content.res.CompatibilityInfo; 22 import android.content.res.Configuration; 23 import android.content.res.TypedArray; 24 import android.graphics.PixelFormat; 25 import android.graphics.drawable.Drawable; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.IBinder; 29 import android.os.SystemProperties; 30 import android.util.Slog; 31 import android.view.accessibility.AccessibilityEvent; 32 33 /** 34 * Abstract base class for a top-level window look and behavior policy. An 35 * instance of this class should be used as the top-level view added to the 36 * window manager. It provides standard UI policies such as a background, title 37 * area, default key processing, etc. 38 * 39 * <p>The only existing implementation of this abstract class is 40 * android.policy.PhoneWindow, which you should instantiate when needing a 41 * Window. Eventually that class will be refactored and a factory method 42 * added for creating Window instances without knowing about a particular 43 * implementation. 44 */ 45 public abstract class Window { 46 /** Flag for the "options panel" feature. This is enabled by default. */ 47 public static final int FEATURE_OPTIONS_PANEL = 0; 48 /** Flag for the "no title" feature, turning off the title at the top 49 * of the screen. */ 50 public static final int FEATURE_NO_TITLE = 1; 51 /** Flag for the progress indicator feature */ 52 public static final int FEATURE_PROGRESS = 2; 53 /** Flag for having an icon on the left side of the title bar */ 54 public static final int FEATURE_LEFT_ICON = 3; 55 /** Flag for having an icon on the right side of the title bar */ 56 public static final int FEATURE_RIGHT_ICON = 4; 57 /** Flag for indeterminate progress */ 58 public static final int FEATURE_INDETERMINATE_PROGRESS = 5; 59 /** Flag for the context menu. This is enabled by default. */ 60 public static final int FEATURE_CONTEXT_MENU = 6; 61 /** Flag for custom title. You cannot combine this feature with other title features. */ 62 public static final int FEATURE_CUSTOM_TITLE = 7; 63 /** 64 * Flag for enabling the Action Bar. 65 * This is enabled by default for some devices. The Action Bar 66 * replaces the title bar and provides an alternate location 67 * for an on-screen menu button on some devices. 68 */ 69 public static final int FEATURE_ACTION_BAR = 8; 70 /** 71 * Flag for requesting an Action Bar that overlays window content. 72 * Normally an Action Bar will sit in the space above window content, but if this 73 * feature is requested along with {@link #FEATURE_ACTION_BAR} it will be layered over 74 * the window content itself. This is useful if you would like your app to have more control 75 * over how the Action Bar is displayed, such as letting application content scroll beneath 76 * an Action Bar with a transparent background or otherwise displaying a transparent/translucent 77 * Action Bar over application content. 78 * 79 * <p>This mode is especially useful with {@link View#SYSTEM_UI_FLAG_FULLSCREEN 80 * View.SYSTEM_UI_FLAG_FULLSCREEN}, which allows you to seamlessly hide the 81 * action bar in conjunction with other screen decorations. 82 * 83 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, when an 84 * ActionBar is in this mode it will adjust the insets provided to 85 * {@link View#fitSystemWindows(android.graphics.Rect) View.fitSystemWindows(Rect)} 86 * to include the content covered by the action bar, so you can do layout within 87 * that space. 88 */ 89 public static final int FEATURE_ACTION_BAR_OVERLAY = 9; 90 /** 91 * Flag for specifying the behavior of action modes when an Action Bar is not present. 92 * If overlay is enabled, the action mode UI will be allowed to cover existing window content. 93 */ 94 public static final int FEATURE_ACTION_MODE_OVERLAY = 10; 95 /** Flag for setting the progress bar's visibility to VISIBLE */ 96 public static final int PROGRESS_VISIBILITY_ON = -1; 97 /** Flag for setting the progress bar's visibility to GONE */ 98 public static final int PROGRESS_VISIBILITY_OFF = -2; 99 /** Flag for setting the progress bar's indeterminate mode on */ 100 public static final int PROGRESS_INDETERMINATE_ON = -3; 101 /** Flag for setting the progress bar's indeterminate mode off */ 102 public static final int PROGRESS_INDETERMINATE_OFF = -4; 103 /** Starting value for the (primary) progress */ 104 public static final int PROGRESS_START = 0; 105 /** Ending value for the (primary) progress */ 106 public static final int PROGRESS_END = 10000; 107 /** Lowest possible value for the secondary progress */ 108 public static final int PROGRESS_SECONDARY_START = 20000; 109 /** Highest possible value for the secondary progress */ 110 public static final int PROGRESS_SECONDARY_END = 30000; 111 112 /** The default features enabled */ 113 @SuppressWarnings({"PointlessBitwiseExpression"}) 114 protected static final int DEFAULT_FEATURES = (1 << FEATURE_OPTIONS_PANEL) | 115 (1 << FEATURE_CONTEXT_MENU); 116 117 /** 118 * The ID that the main layout in the XML layout file should have. 119 */ 120 public static final int ID_ANDROID_CONTENT = com.android.internal.R.id.content; 121 122 private final Context mContext; 123 124 private TypedArray mWindowStyle; 125 private Callback mCallback; 126 private WindowManager mWindowManager; 127 private IBinder mAppToken; 128 private String mAppName; 129 private Window mContainer; 130 private Window mActiveChild; 131 private boolean mIsActive = false; 132 private boolean mHasChildren = false; 133 private boolean mCloseOnTouchOutside = false; 134 private boolean mSetCloseOnTouchOutside = false; 135 private int mForcedWindowFlags = 0; 136 137 private int mFeatures = DEFAULT_FEATURES; 138 private int mLocalFeatures = DEFAULT_FEATURES; 139 140 private boolean mHaveWindowFormat = false; 141 private boolean mHaveDimAmount = false; 142 private int mDefaultWindowFormat = PixelFormat.OPAQUE; 143 144 private boolean mHasSoftInputMode = false; 145 146 private boolean mDestroyed; 147 148 // The current window attributes. 149 private final WindowManager.LayoutParams mWindowAttributes = 150 new WindowManager.LayoutParams(); 151 152 /** 153 * API from a Window back to its caller. This allows the client to 154 * intercept key dispatching, panels and menus, etc. 155 */ 156 public interface Callback { 157 /** 158 * Called to process key events. At the very least your 159 * implementation must call 160 * {@link android.view.Window#superDispatchKeyEvent} to do the 161 * standard key processing. 162 * 163 * @param event The key event. 164 * 165 * @return boolean Return true if this event was consumed. 166 */ dispatchKeyEvent(KeyEvent event)167 public boolean dispatchKeyEvent(KeyEvent event); 168 169 /** 170 * Called to process a key shortcut event. 171 * At the very least your implementation must call 172 * {@link android.view.Window#superDispatchKeyShortcutEvent} to do the 173 * standard key shortcut processing. 174 * 175 * @param event The key shortcut event. 176 * @return True if this event was consumed. 177 */ dispatchKeyShortcutEvent(KeyEvent event)178 public boolean dispatchKeyShortcutEvent(KeyEvent event); 179 180 /** 181 * Called to process touch screen events. At the very least your 182 * implementation must call 183 * {@link android.view.Window#superDispatchTouchEvent} to do the 184 * standard touch screen processing. 185 * 186 * @param event The touch screen event. 187 * 188 * @return boolean Return true if this event was consumed. 189 */ dispatchTouchEvent(MotionEvent event)190 public boolean dispatchTouchEvent(MotionEvent event); 191 192 /** 193 * Called to process trackball events. At the very least your 194 * implementation must call 195 * {@link android.view.Window#superDispatchTrackballEvent} to do the 196 * standard trackball processing. 197 * 198 * @param event The trackball event. 199 * 200 * @return boolean Return true if this event was consumed. 201 */ dispatchTrackballEvent(MotionEvent event)202 public boolean dispatchTrackballEvent(MotionEvent event); 203 204 /** 205 * Called to process generic motion events. At the very least your 206 * implementation must call 207 * {@link android.view.Window#superDispatchGenericMotionEvent} to do the 208 * standard processing. 209 * 210 * @param event The generic motion event. 211 * 212 * @return boolean Return true if this event was consumed. 213 */ dispatchGenericMotionEvent(MotionEvent event)214 public boolean dispatchGenericMotionEvent(MotionEvent event); 215 216 /** 217 * Called to process population of {@link AccessibilityEvent}s. 218 * 219 * @param event The event. 220 * 221 * @return boolean Return true if event population was completed. 222 */ dispatchPopulateAccessibilityEvent(AccessibilityEvent event)223 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event); 224 225 /** 226 * Instantiate the view to display in the panel for 'featureId'. 227 * You can return null, in which case the default content (typically 228 * a menu) will be created for you. 229 * 230 * @param featureId Which panel is being created. 231 * 232 * @return view The top-level view to place in the panel. 233 * 234 * @see #onPreparePanel 235 */ onCreatePanelView(int featureId)236 public View onCreatePanelView(int featureId); 237 238 /** 239 * Initialize the contents of the menu for panel 'featureId'. This is 240 * called if onCreatePanelView() returns null, giving you a standard 241 * menu in which you can place your items. It is only called once for 242 * the panel, the first time it is shown. 243 * 244 * <p>You can safely hold on to <var>menu</var> (and any items created 245 * from it), making modifications to it as desired, until the next 246 * time onCreatePanelMenu() is called for this feature. 247 * 248 * @param featureId The panel being created. 249 * @param menu The menu inside the panel. 250 * 251 * @return boolean You must return true for the panel to be displayed; 252 * if you return false it will not be shown. 253 */ onCreatePanelMenu(int featureId, Menu menu)254 public boolean onCreatePanelMenu(int featureId, Menu menu); 255 256 /** 257 * Prepare a panel to be displayed. This is called right before the 258 * panel window is shown, every time it is shown. 259 * 260 * @param featureId The panel that is being displayed. 261 * @param view The View that was returned by onCreatePanelView(). 262 * @param menu If onCreatePanelView() returned null, this is the Menu 263 * being displayed in the panel. 264 * 265 * @return boolean You must return true for the panel to be displayed; 266 * if you return false it will not be shown. 267 * 268 * @see #onCreatePanelView 269 */ onPreparePanel(int featureId, View view, Menu menu)270 public boolean onPreparePanel(int featureId, View view, Menu menu); 271 272 /** 273 * Called when a panel's menu is opened by the user. This may also be 274 * called when the menu is changing from one type to another (for 275 * example, from the icon menu to the expanded menu). 276 * 277 * @param featureId The panel that the menu is in. 278 * @param menu The menu that is opened. 279 * @return Return true to allow the menu to open, or false to prevent 280 * the menu from opening. 281 */ onMenuOpened(int featureId, Menu menu)282 public boolean onMenuOpened(int featureId, Menu menu); 283 284 /** 285 * Called when a panel's menu item has been selected by the user. 286 * 287 * @param featureId The panel that the menu is in. 288 * @param item The menu item that was selected. 289 * 290 * @return boolean Return true to finish processing of selection, or 291 * false to perform the normal menu handling (calling its 292 * Runnable or sending a Message to its target Handler). 293 */ onMenuItemSelected(int featureId, MenuItem item)294 public boolean onMenuItemSelected(int featureId, MenuItem item); 295 296 /** 297 * This is called whenever the current window attributes change. 298 * 299 */ onWindowAttributesChanged(WindowManager.LayoutParams attrs)300 public void onWindowAttributesChanged(WindowManager.LayoutParams attrs); 301 302 /** 303 * This hook is called whenever the content view of the screen changes 304 * (due to a call to 305 * {@link Window#setContentView(View, android.view.ViewGroup.LayoutParams) 306 * Window.setContentView} or 307 * {@link Window#addContentView(View, android.view.ViewGroup.LayoutParams) 308 * Window.addContentView}). 309 */ onContentChanged()310 public void onContentChanged(); 311 312 /** 313 * This hook is called whenever the window focus changes. See 314 * {@link View#onWindowFocusChanged(boolean) 315 * View.onWindowFocusChanged(boolean)} for more information. 316 * 317 * @param hasFocus Whether the window now has focus. 318 */ onWindowFocusChanged(boolean hasFocus)319 public void onWindowFocusChanged(boolean hasFocus); 320 321 /** 322 * Called when the window has been attached to the window manager. 323 * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} 324 * for more information. 325 */ onAttachedToWindow()326 public void onAttachedToWindow(); 327 328 /** 329 * Called when the window has been attached to the window manager. 330 * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} 331 * for more information. 332 */ onDetachedFromWindow()333 public void onDetachedFromWindow(); 334 335 /** 336 * Called when a panel is being closed. If another logical subsequent 337 * panel is being opened (and this panel is being closed to make room for the subsequent 338 * panel), this method will NOT be called. 339 * 340 * @param featureId The panel that is being displayed. 341 * @param menu If onCreatePanelView() returned null, this is the Menu 342 * being displayed in the panel. 343 */ onPanelClosed(int featureId, Menu menu)344 public void onPanelClosed(int featureId, Menu menu); 345 346 /** 347 * Called when the user signals the desire to start a search. 348 * 349 * @return true if search launched, false if activity refuses (blocks) 350 * 351 * @see android.app.Activity#onSearchRequested() 352 */ onSearchRequested()353 public boolean onSearchRequested(); 354 355 /** 356 * Called when an action mode is being started for this window. Gives the 357 * callback an opportunity to handle the action mode in its own unique and 358 * beautiful way. If this method returns null the system can choose a way 359 * to present the mode or choose not to start the mode at all. 360 * 361 * @param callback Callback to control the lifecycle of this action mode 362 * @return The ActionMode that was started, or null if the system should present it 363 */ onWindowStartingActionMode(ActionMode.Callback callback)364 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback); 365 366 /** 367 * Called when an action mode has been started. The appropriate mode callback 368 * method will have already been invoked. 369 * 370 * @param mode The new mode that has just been started. 371 */ onActionModeStarted(ActionMode mode)372 public void onActionModeStarted(ActionMode mode); 373 374 /** 375 * Called when an action mode has been finished. The appropriate mode callback 376 * method will have already been invoked. 377 * 378 * @param mode The mode that was just finished. 379 */ onActionModeFinished(ActionMode mode)380 public void onActionModeFinished(ActionMode mode); 381 } 382 Window(Context context)383 public Window(Context context) { 384 mContext = context; 385 } 386 387 /** 388 * Return the Context this window policy is running in, for retrieving 389 * resources and other information. 390 * 391 * @return Context The Context that was supplied to the constructor. 392 */ getContext()393 public final Context getContext() { 394 return mContext; 395 } 396 397 /** 398 * Return the {@link android.R.styleable#Window} attributes from this 399 * window's theme. 400 */ getWindowStyle()401 public final TypedArray getWindowStyle() { 402 synchronized (this) { 403 if (mWindowStyle == null) { 404 mWindowStyle = mContext.obtainStyledAttributes( 405 com.android.internal.R.styleable.Window); 406 } 407 return mWindowStyle; 408 } 409 } 410 411 /** 412 * Set the container for this window. If not set, the DecorWindow 413 * operates as a top-level window; otherwise, it negotiates with the 414 * container to display itself appropriately. 415 * 416 * @param container The desired containing Window. 417 */ setContainer(Window container)418 public void setContainer(Window container) { 419 mContainer = container; 420 if (container != null) { 421 // Embedded screens never have a title. 422 mFeatures |= 1<<FEATURE_NO_TITLE; 423 mLocalFeatures |= 1<<FEATURE_NO_TITLE; 424 container.mHasChildren = true; 425 } 426 } 427 428 /** 429 * Return the container for this Window. 430 * 431 * @return Window The containing window, or null if this is a 432 * top-level window. 433 */ getContainer()434 public final Window getContainer() { 435 return mContainer; 436 } 437 hasChildren()438 public final boolean hasChildren() { 439 return mHasChildren; 440 } 441 442 /** @hide */ destroy()443 public final void destroy() { 444 mDestroyed = true; 445 } 446 447 /** @hide */ isDestroyed()448 public final boolean isDestroyed() { 449 return mDestroyed; 450 } 451 452 /** 453 * Set the window manager for use by this Window to, for example, 454 * display panels. This is <em>not</em> used for displaying the 455 * Window itself -- that must be done by the client. 456 * 457 * @param wm The ViewManager for adding new windows. 458 */ setWindowManager(WindowManager wm, IBinder appToken, String appName)459 public void setWindowManager(WindowManager wm, IBinder appToken, String appName) { 460 setWindowManager(wm, appToken, appName, false); 461 } 462 463 /** 464 * Set the window manager for use by this Window to, for example, 465 * display panels. This is <em>not</em> used for displaying the 466 * Window itself -- that must be done by the client. 467 * 468 * @param wm The ViewManager for adding new windows. 469 */ setWindowManager(WindowManager wm, IBinder appToken, String appName, boolean hardwareAccelerated)470 public void setWindowManager(WindowManager wm, IBinder appToken, String appName, 471 boolean hardwareAccelerated) { 472 mAppToken = appToken; 473 mAppName = appName; 474 if (wm == null) { 475 wm = WindowManagerImpl.getDefault(); 476 } 477 mWindowManager = new LocalWindowManager(wm, hardwareAccelerated); 478 } 479 getCompatInfo(Context context)480 static CompatibilityInfoHolder getCompatInfo(Context context) { 481 Application app = (Application)context.getApplicationContext(); 482 return app != null ? app.mLoadedApk.mCompatibilityInfo : new CompatibilityInfoHolder(); 483 } 484 485 private class LocalWindowManager extends WindowManagerImpl.CompatModeWrapper { 486 private static final String PROPERTY_HARDWARE_UI = "persist.sys.ui.hw"; 487 488 private final boolean mHardwareAccelerated; 489 LocalWindowManager(WindowManager wm, boolean hardwareAccelerated)490 LocalWindowManager(WindowManager wm, boolean hardwareAccelerated) { 491 super(wm, getCompatInfo(mContext)); 492 mHardwareAccelerated = hardwareAccelerated || 493 SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false); 494 } 495 isHardwareAccelerated()496 public boolean isHardwareAccelerated() { 497 return mHardwareAccelerated; 498 } 499 addView(View view, ViewGroup.LayoutParams params)500 public final void addView(View view, ViewGroup.LayoutParams params) { 501 // Let this throw an exception on a bad params. 502 WindowManager.LayoutParams wp = (WindowManager.LayoutParams)params; 503 CharSequence curTitle = wp.getTitle(); 504 if (wp.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && 505 wp.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) { 506 if (wp.token == null) { 507 View decor = peekDecorView(); 508 if (decor != null) { 509 wp.token = decor.getWindowToken(); 510 } 511 } 512 if (curTitle == null || curTitle.length() == 0) { 513 String title; 514 if (wp.type == WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA) { 515 title="Media"; 516 } else if (wp.type == WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY) { 517 title="MediaOvr"; 518 } else if (wp.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) { 519 title="Panel"; 520 } else if (wp.type == WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL) { 521 title="SubPanel"; 522 } else if (wp.type == WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG) { 523 title="AtchDlg"; 524 } else { 525 title=Integer.toString(wp.type); 526 } 527 if (mAppName != null) { 528 title += ":" + mAppName; 529 } 530 wp.setTitle(title); 531 } 532 } else { 533 if (wp.token == null) { 534 wp.token = mContainer == null ? mAppToken : mContainer.mAppToken; 535 } 536 if ((curTitle == null || curTitle.length() == 0) 537 && mAppName != null) { 538 wp.setTitle(mAppName); 539 } 540 } 541 if (wp.packageName == null) { 542 wp.packageName = mContext.getPackageName(); 543 } 544 if (mHardwareAccelerated) { 545 wp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; 546 } 547 super.addView(view, params); 548 } 549 } 550 551 /** 552 * Return the window manager allowing this Window to display its own 553 * windows. 554 * 555 * @return WindowManager The ViewManager. 556 */ getWindowManager()557 public WindowManager getWindowManager() { 558 return mWindowManager; 559 } 560 561 /** 562 * Set the Callback interface for this window, used to intercept key 563 * events and other dynamic operations in the window. 564 * 565 * @param callback The desired Callback interface. 566 */ setCallback(Callback callback)567 public void setCallback(Callback callback) { 568 mCallback = callback; 569 } 570 571 /** 572 * Return the current Callback interface for this window. 573 */ getCallback()574 public final Callback getCallback() { 575 return mCallback; 576 } 577 578 /** 579 * Take ownership of this window's surface. The window's view hierarchy 580 * will no longer draw into the surface, though it will otherwise continue 581 * to operate (such as for receiving input events). The given SurfaceHolder 582 * callback will be used to tell you about state changes to the surface. 583 */ takeSurface(SurfaceHolder.Callback2 callback)584 public abstract void takeSurface(SurfaceHolder.Callback2 callback); 585 586 /** 587 * Take ownership of this window's InputQueue. The window will no 588 * longer read and dispatch input events from the queue; it is your 589 * responsibility to do so. 590 */ takeInputQueue(InputQueue.Callback callback)591 public abstract void takeInputQueue(InputQueue.Callback callback); 592 593 /** 594 * Return whether this window is being displayed with a floating style 595 * (based on the {@link android.R.attr#windowIsFloating} attribute in 596 * the style/theme). 597 * 598 * @return Returns true if the window is configured to be displayed floating 599 * on top of whatever is behind it. 600 */ isFloating()601 public abstract boolean isFloating(); 602 603 /** 604 * Set the width and height layout parameters of the window. The default 605 * for both of these is MATCH_PARENT; you can change them to WRAP_CONTENT 606 * or an absolute value to make a window that is not full-screen. 607 * 608 * @param width The desired layout width of the window. 609 * @param height The desired layout height of the window. 610 * 611 * @see ViewGroup.LayoutParams#height 612 * @see ViewGroup.LayoutParams#width 613 */ setLayout(int width, int height)614 public void setLayout(int width, int height) { 615 final WindowManager.LayoutParams attrs = getAttributes(); 616 attrs.width = width; 617 attrs.height = height; 618 if (mCallback != null) { 619 mCallback.onWindowAttributesChanged(attrs); 620 } 621 } 622 623 /** 624 * Set the gravity of the window, as per the Gravity constants. This 625 * controls how the window manager is positioned in the overall window; it 626 * is only useful when using WRAP_CONTENT for the layout width or height. 627 * 628 * @param gravity The desired gravity constant. 629 * 630 * @see Gravity 631 * @see #setLayout 632 */ setGravity(int gravity)633 public void setGravity(int gravity) 634 { 635 final WindowManager.LayoutParams attrs = getAttributes(); 636 attrs.gravity = gravity; 637 if (mCallback != null) { 638 mCallback.onWindowAttributesChanged(attrs); 639 } 640 } 641 642 /** 643 * Set the type of the window, as per the WindowManager.LayoutParams 644 * types. 645 * 646 * @param type The new window type (see WindowManager.LayoutParams). 647 */ setType(int type)648 public void setType(int type) { 649 final WindowManager.LayoutParams attrs = getAttributes(); 650 attrs.type = type; 651 if (mCallback != null) { 652 mCallback.onWindowAttributesChanged(attrs); 653 } 654 } 655 656 /** 657 * Set the format of window, as per the PixelFormat types. This overrides 658 * the default format that is selected by the Window based on its 659 * window decorations. 660 * 661 * @param format The new window format (see PixelFormat). Use 662 * PixelFormat.UNKNOWN to allow the Window to select 663 * the format. 664 * 665 * @see PixelFormat 666 */ setFormat(int format)667 public void setFormat(int format) { 668 final WindowManager.LayoutParams attrs = getAttributes(); 669 if (format != PixelFormat.UNKNOWN) { 670 attrs.format = format; 671 mHaveWindowFormat = true; 672 } else { 673 attrs.format = mDefaultWindowFormat; 674 mHaveWindowFormat = false; 675 } 676 if (mCallback != null) { 677 mCallback.onWindowAttributesChanged(attrs); 678 } 679 } 680 681 /** 682 * Specify custom animations to use for the window, as per 683 * {@link WindowManager.LayoutParams#windowAnimations 684 * WindowManager.LayoutParams.windowAnimations}. Providing anything besides 685 * 0 here will override the animations the window would 686 * normally retrieve from its theme. 687 */ setWindowAnimations(int resId)688 public void setWindowAnimations(int resId) { 689 final WindowManager.LayoutParams attrs = getAttributes(); 690 attrs.windowAnimations = resId; 691 if (mCallback != null) { 692 mCallback.onWindowAttributesChanged(attrs); 693 } 694 } 695 696 /** 697 * Specify an explicit soft input mode to use for the window, as per 698 * {@link WindowManager.LayoutParams#softInputMode 699 * WindowManager.LayoutParams.softInputMode}. Providing anything besides 700 * "unspecified" here will override the input mode the window would 701 * normally retrieve from its theme. 702 */ setSoftInputMode(int mode)703 public void setSoftInputMode(int mode) { 704 final WindowManager.LayoutParams attrs = getAttributes(); 705 if (mode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { 706 attrs.softInputMode = mode; 707 mHasSoftInputMode = true; 708 } else { 709 mHasSoftInputMode = false; 710 } 711 if (mCallback != null) { 712 mCallback.onWindowAttributesChanged(attrs); 713 } 714 } 715 716 /** 717 * Convenience function to set the flag bits as specified in flags, as 718 * per {@link #setFlags}. 719 * @param flags The flag bits to be set. 720 * @see #setFlags 721 */ addFlags(int flags)722 public void addFlags(int flags) { 723 setFlags(flags, flags); 724 } 725 726 /** 727 * Convenience function to clear the flag bits as specified in flags, as 728 * per {@link #setFlags}. 729 * @param flags The flag bits to be cleared. 730 * @see #setFlags 731 */ clearFlags(int flags)732 public void clearFlags(int flags) { 733 setFlags(0, flags); 734 } 735 736 /** 737 * Set the flags of the window, as per the 738 * {@link WindowManager.LayoutParams WindowManager.LayoutParams} 739 * flags. 740 * 741 * <p>Note that some flags must be set before the window decoration is 742 * created (by the first call to 743 * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} or 744 * {@link #getDecorView()}: 745 * {@link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN} and 746 * {@link WindowManager.LayoutParams#FLAG_LAYOUT_INSET_DECOR}. These 747 * will be set for you based on the {@link android.R.attr#windowIsFloating} 748 * attribute. 749 * 750 * @param flags The new window flags (see WindowManager.LayoutParams). 751 * @param mask Which of the window flag bits to modify. 752 */ setFlags(int flags, int mask)753 public void setFlags(int flags, int mask) { 754 final WindowManager.LayoutParams attrs = getAttributes(); 755 attrs.flags = (attrs.flags&~mask) | (flags&mask); 756 if ((mask&WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY) != 0) { 757 attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SET_NEEDS_MENU_KEY; 758 } 759 mForcedWindowFlags |= mask; 760 if (mCallback != null) { 761 mCallback.onWindowAttributesChanged(attrs); 762 } 763 } 764 765 /** 766 * Set the amount of dim behind the window when using 767 * {@link WindowManager.LayoutParams#FLAG_DIM_BEHIND}. This overrides 768 * the default dim amount of that is selected by the Window based on 769 * its theme. 770 * 771 * @param amount The new dim amount, from 0 for no dim to 1 for full dim. 772 */ setDimAmount(float amount)773 public void setDimAmount(float amount) { 774 final WindowManager.LayoutParams attrs = getAttributes(); 775 attrs.dimAmount = amount; 776 mHaveDimAmount = true; 777 if (mCallback != null) { 778 mCallback.onWindowAttributesChanged(attrs); 779 } 780 } 781 782 /** 783 * Specify custom window attributes. <strong>PLEASE NOTE:</strong> the 784 * layout params you give here should generally be from values previously 785 * retrieved with {@link #getAttributes()}; you probably do not want to 786 * blindly create and apply your own, since this will blow away any values 787 * set by the framework that you are not interested in. 788 * 789 * @param a The new window attributes, which will completely override any 790 * current values. 791 */ setAttributes(WindowManager.LayoutParams a)792 public void setAttributes(WindowManager.LayoutParams a) { 793 mWindowAttributes.copyFrom(a); 794 if (mCallback != null) { 795 mCallback.onWindowAttributesChanged(mWindowAttributes); 796 } 797 } 798 799 /** 800 * Retrieve the current window attributes associated with this panel. 801 * 802 * @return WindowManager.LayoutParams Either the existing window 803 * attributes object, or a freshly created one if there is none. 804 */ getAttributes()805 public final WindowManager.LayoutParams getAttributes() { 806 return mWindowAttributes; 807 } 808 809 /** 810 * Return the window flags that have been explicitly set by the client, 811 * so will not be modified by {@link #getDecorView}. 812 */ getForcedWindowFlags()813 protected final int getForcedWindowFlags() { 814 return mForcedWindowFlags; 815 } 816 817 /** 818 * Has the app specified their own soft input mode? 819 */ hasSoftInputMode()820 protected final boolean hasSoftInputMode() { 821 return mHasSoftInputMode; 822 } 823 824 /** @hide */ setCloseOnTouchOutside(boolean close)825 public void setCloseOnTouchOutside(boolean close) { 826 mCloseOnTouchOutside = close; 827 mSetCloseOnTouchOutside = true; 828 } 829 830 /** @hide */ setCloseOnTouchOutsideIfNotSet(boolean close)831 public void setCloseOnTouchOutsideIfNotSet(boolean close) { 832 if (!mSetCloseOnTouchOutside) { 833 mCloseOnTouchOutside = close; 834 mSetCloseOnTouchOutside = true; 835 } 836 } 837 838 /** @hide */ alwaysReadCloseOnTouchAttr()839 public abstract void alwaysReadCloseOnTouchAttr(); 840 841 /** @hide */ shouldCloseOnTouch(Context context, MotionEvent event)842 public boolean shouldCloseOnTouch(Context context, MotionEvent event) { 843 if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN 844 && isOutOfBounds(context, event) && peekDecorView() != null) { 845 return true; 846 } 847 return false; 848 } 849 isOutOfBounds(Context context, MotionEvent event)850 private boolean isOutOfBounds(Context context, MotionEvent event) { 851 final int x = (int) event.getX(); 852 final int y = (int) event.getY(); 853 final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop(); 854 final View decorView = getDecorView(); 855 return (x < -slop) || (y < -slop) 856 || (x > (decorView.getWidth()+slop)) 857 || (y > (decorView.getHeight()+slop)); 858 } 859 860 /** 861 * Enable extended screen features. This must be called before 862 * setContentView(). May be called as many times as desired as long as it 863 * is before setContentView(). If not called, no extended features 864 * will be available. You can not turn off a feature once it is requested. 865 * You canot use other title features with {@link #FEATURE_CUSTOM_TITLE}. 866 * 867 * @param featureId The desired features, defined as constants by Window. 868 * @return The features that are now set. 869 */ requestFeature(int featureId)870 public boolean requestFeature(int featureId) { 871 final int flag = 1<<featureId; 872 mFeatures |= flag; 873 mLocalFeatures |= mContainer != null ? (flag&~mContainer.mFeatures) : flag; 874 return (mFeatures&flag) != 0; 875 } 876 877 /** 878 * @hide Used internally to help resolve conflicting features. 879 */ removeFeature(int featureId)880 protected void removeFeature(int featureId) { 881 final int flag = 1<<featureId; 882 mFeatures &= ~flag; 883 mLocalFeatures &= ~(mContainer != null ? (flag&~mContainer.mFeatures) : flag); 884 } 885 makeActive()886 public final void makeActive() { 887 if (mContainer != null) { 888 if (mContainer.mActiveChild != null) { 889 mContainer.mActiveChild.mIsActive = false; 890 } 891 mContainer.mActiveChild = this; 892 } 893 mIsActive = true; 894 onActive(); 895 } 896 isActive()897 public final boolean isActive() 898 { 899 return mIsActive; 900 } 901 902 /** 903 * Finds a view that was identified by the id attribute from the XML that 904 * was processed in {@link android.app.Activity#onCreate}. This will 905 * implicitly call {@link #getDecorView} for you, with all of the 906 * associated side-effects. 907 * 908 * @return The view if found or null otherwise. 909 */ findViewById(int id)910 public View findViewById(int id) { 911 return getDecorView().findViewById(id); 912 } 913 914 /** 915 * Convenience for 916 * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} 917 * to set the screen content from a layout resource. The resource will be 918 * inflated, adding all top-level views to the screen. 919 * 920 * @param layoutResID Resource ID to be inflated. 921 * @see #setContentView(View, android.view.ViewGroup.LayoutParams) 922 */ setContentView(int layoutResID)923 public abstract void setContentView(int layoutResID); 924 925 /** 926 * Convenience for 927 * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} 928 * set the screen content to an explicit view. This view is placed 929 * directly into the screen's view hierarchy. It can itself be a complex 930 * view hierarhcy. 931 * 932 * @param view The desired content to display. 933 * @see #setContentView(View, android.view.ViewGroup.LayoutParams) 934 */ setContentView(View view)935 public abstract void setContentView(View view); 936 937 /** 938 * Set the screen content to an explicit view. This view is placed 939 * directly into the screen's view hierarchy. It can itself be a complex 940 * view hierarchy. 941 * 942 * <p>Note that calling this function "locks in" various characteristics 943 * of the window that can not, from this point forward, be changed: the 944 * features that have been requested with {@link #requestFeature(int)}, 945 * and certain window flags as described in {@link #setFlags(int, int)}. 946 * 947 * @param view The desired content to display. 948 * @param params Layout parameters for the view. 949 */ setContentView(View view, ViewGroup.LayoutParams params)950 public abstract void setContentView(View view, ViewGroup.LayoutParams params); 951 952 /** 953 * Variation on 954 * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} 955 * to add an additional content view to the screen. Added after any existing 956 * ones in the screen -- existing views are NOT removed. 957 * 958 * @param view The desired content to display. 959 * @param params Layout parameters for the view. 960 */ addContentView(View view, ViewGroup.LayoutParams params)961 public abstract void addContentView(View view, ViewGroup.LayoutParams params); 962 963 /** 964 * Return the view in this Window that currently has focus, or null if 965 * there are none. Note that this does not look in any containing 966 * Window. 967 * 968 * @return View The current View with focus or null. 969 */ getCurrentFocus()970 public abstract View getCurrentFocus(); 971 972 /** 973 * Quick access to the {@link LayoutInflater} instance that this Window 974 * retrieved from its Context. 975 * 976 * @return LayoutInflater The shared LayoutInflater. 977 */ getLayoutInflater()978 public abstract LayoutInflater getLayoutInflater(); 979 setTitle(CharSequence title)980 public abstract void setTitle(CharSequence title); 981 setTitleColor(int textColor)982 public abstract void setTitleColor(int textColor); 983 openPanel(int featureId, KeyEvent event)984 public abstract void openPanel(int featureId, KeyEvent event); 985 closePanel(int featureId)986 public abstract void closePanel(int featureId); 987 togglePanel(int featureId, KeyEvent event)988 public abstract void togglePanel(int featureId, KeyEvent event); 989 invalidatePanelMenu(int featureId)990 public abstract void invalidatePanelMenu(int featureId); 991 performPanelShortcut(int featureId, int keyCode, KeyEvent event, int flags)992 public abstract boolean performPanelShortcut(int featureId, 993 int keyCode, 994 KeyEvent event, 995 int flags); performPanelIdentifierAction(int featureId, int id, int flags)996 public abstract boolean performPanelIdentifierAction(int featureId, 997 int id, 998 int flags); 999 closeAllPanels()1000 public abstract void closeAllPanels(); 1001 performContextMenuIdentifierAction(int id, int flags)1002 public abstract boolean performContextMenuIdentifierAction(int id, int flags); 1003 1004 /** 1005 * Should be called when the configuration is changed. 1006 * 1007 * @param newConfig The new configuration. 1008 */ onConfigurationChanged(Configuration newConfig)1009 public abstract void onConfigurationChanged(Configuration newConfig); 1010 1011 /** 1012 * Change the background of this window to a Drawable resource. Setting the 1013 * background to null will make the window be opaque. To make the window 1014 * transparent, you can use an empty drawable (for instance a ColorDrawable 1015 * with the color 0 or the system drawable android:drawable/empty.) 1016 * 1017 * @param resid The resource identifier of a drawable resource which will be 1018 * installed as the new background. 1019 */ setBackgroundDrawableResource(int resid)1020 public void setBackgroundDrawableResource(int resid) 1021 { 1022 setBackgroundDrawable(mContext.getResources().getDrawable(resid)); 1023 } 1024 1025 /** 1026 * Change the background of this window to a custom Drawable. Setting the 1027 * background to null will make the window be opaque. To make the window 1028 * transparent, you can use an empty drawable (for instance a ColorDrawable 1029 * with the color 0 or the system drawable android:drawable/empty.) 1030 * 1031 * @param drawable The new Drawable to use for this window's background. 1032 */ setBackgroundDrawable(Drawable drawable)1033 public abstract void setBackgroundDrawable(Drawable drawable); 1034 1035 /** 1036 * Set the value for a drawable feature of this window, from a resource 1037 * identifier. You must have called requestFeauture(featureId) before 1038 * calling this function. 1039 * 1040 * @see android.content.res.Resources#getDrawable(int) 1041 * 1042 * @param featureId The desired drawable feature to change, defined as a 1043 * constant by Window. 1044 * @param resId Resource identifier of the desired image. 1045 */ setFeatureDrawableResource(int featureId, int resId)1046 public abstract void setFeatureDrawableResource(int featureId, int resId); 1047 1048 /** 1049 * Set the value for a drawable feature of this window, from a URI. You 1050 * must have called requestFeature(featureId) before calling this 1051 * function. 1052 * 1053 * <p>The only URI currently supported is "content:", specifying an image 1054 * in a content provider. 1055 * 1056 * @see android.widget.ImageView#setImageURI 1057 * 1058 * @param featureId The desired drawable feature to change. Features are 1059 * constants defined by Window. 1060 * @param uri The desired URI. 1061 */ setFeatureDrawableUri(int featureId, Uri uri)1062 public abstract void setFeatureDrawableUri(int featureId, Uri uri); 1063 1064 /** 1065 * Set an explicit Drawable value for feature of this window. You must 1066 * have called requestFeature(featureId) before calling this function. 1067 * 1068 * @param featureId The desired drawable feature to change. 1069 * Features are constants defined by Window. 1070 * @param drawable A Drawable object to display. 1071 */ setFeatureDrawable(int featureId, Drawable drawable)1072 public abstract void setFeatureDrawable(int featureId, Drawable drawable); 1073 1074 /** 1075 * Set a custom alpha value for the given drawale feature, controlling how 1076 * much the background is visible through it. 1077 * 1078 * @param featureId The desired drawable feature to change. 1079 * Features are constants defined by Window. 1080 * @param alpha The alpha amount, 0 is completely transparent and 255 is 1081 * completely opaque. 1082 */ setFeatureDrawableAlpha(int featureId, int alpha)1083 public abstract void setFeatureDrawableAlpha(int featureId, int alpha); 1084 1085 /** 1086 * Set the integer value for a feature. The range of the value depends on 1087 * the feature being set. For FEATURE_PROGRESSS, it should go from 0 to 1088 * 10000. At 10000 the progress is complete and the indicator hidden. 1089 * 1090 * @param featureId The desired feature to change. 1091 * Features are constants defined by Window. 1092 * @param value The value for the feature. The interpretation of this 1093 * value is feature-specific. 1094 */ setFeatureInt(int featureId, int value)1095 public abstract void setFeatureInt(int featureId, int value); 1096 1097 /** 1098 * Request that key events come to this activity. Use this if your 1099 * activity has no views with focus, but the activity still wants 1100 * a chance to process key events. 1101 */ takeKeyEvents(boolean get)1102 public abstract void takeKeyEvents(boolean get); 1103 1104 /** 1105 * Used by custom windows, such as Dialog, to pass the key press event 1106 * further down the view hierarchy. Application developers should 1107 * not need to implement or call this. 1108 * 1109 */ superDispatchKeyEvent(KeyEvent event)1110 public abstract boolean superDispatchKeyEvent(KeyEvent event); 1111 1112 /** 1113 * Used by custom windows, such as Dialog, to pass the key shortcut press event 1114 * further down the view hierarchy. Application developers should 1115 * not need to implement or call this. 1116 * 1117 */ superDispatchKeyShortcutEvent(KeyEvent event)1118 public abstract boolean superDispatchKeyShortcutEvent(KeyEvent event); 1119 1120 /** 1121 * Used by custom windows, such as Dialog, to pass the touch screen event 1122 * further down the view hierarchy. Application developers should 1123 * not need to implement or call this. 1124 * 1125 */ superDispatchTouchEvent(MotionEvent event)1126 public abstract boolean superDispatchTouchEvent(MotionEvent event); 1127 1128 /** 1129 * Used by custom windows, such as Dialog, to pass the trackball event 1130 * further down the view hierarchy. Application developers should 1131 * not need to implement or call this. 1132 * 1133 */ superDispatchTrackballEvent(MotionEvent event)1134 public abstract boolean superDispatchTrackballEvent(MotionEvent event); 1135 1136 /** 1137 * Used by custom windows, such as Dialog, to pass the generic motion event 1138 * further down the view hierarchy. Application developers should 1139 * not need to implement or call this. 1140 * 1141 */ superDispatchGenericMotionEvent(MotionEvent event)1142 public abstract boolean superDispatchGenericMotionEvent(MotionEvent event); 1143 1144 /** 1145 * Retrieve the top-level window decor view (containing the standard 1146 * window frame/decorations and the client's content inside of that), which 1147 * can be added as a window to the window manager. 1148 * 1149 * <p><em>Note that calling this function for the first time "locks in" 1150 * various window characteristics as described in 1151 * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}.</em></p> 1152 * 1153 * @return Returns the top-level window decor view. 1154 */ getDecorView()1155 public abstract View getDecorView(); 1156 1157 /** 1158 * Retrieve the current decor view, but only if it has already been created; 1159 * otherwise returns null. 1160 * 1161 * @return Returns the top-level window decor or null. 1162 * @see #getDecorView 1163 */ peekDecorView()1164 public abstract View peekDecorView(); 1165 saveHierarchyState()1166 public abstract Bundle saveHierarchyState(); 1167 restoreHierarchyState(Bundle savedInstanceState)1168 public abstract void restoreHierarchyState(Bundle savedInstanceState); 1169 onActive()1170 protected abstract void onActive(); 1171 1172 /** 1173 * Return the feature bits that are enabled. This is the set of features 1174 * that were given to requestFeature(), and are being handled by this 1175 * Window itself or its container. That is, it is the set of 1176 * requested features that you can actually use. 1177 * 1178 * <p>To do: add a public version of this API that allows you to check for 1179 * features by their feature ID. 1180 * 1181 * @return int The feature bits. 1182 */ getFeatures()1183 protected final int getFeatures() 1184 { 1185 return mFeatures; 1186 } 1187 1188 /** 1189 * Query for the availability of a certain feature. 1190 * 1191 * @param feature The feature ID to check 1192 * @return true if the feature is enabled, false otherwise. 1193 */ hasFeature(int feature)1194 public boolean hasFeature(int feature) { 1195 return (getFeatures() & (1 << feature)) != 0; 1196 } 1197 1198 /** 1199 * Return the feature bits that are being implemented by this Window. 1200 * This is the set of features that were given to requestFeature(), and are 1201 * being handled by only this Window itself, not by its containers. 1202 * 1203 * @return int The feature bits. 1204 */ getLocalFeatures()1205 protected final int getLocalFeatures() 1206 { 1207 return mLocalFeatures; 1208 } 1209 1210 /** 1211 * Set the default format of window, as per the PixelFormat types. This 1212 * is the format that will be used unless the client specifies in explicit 1213 * format with setFormat(); 1214 * 1215 * @param format The new window format (see PixelFormat). 1216 * 1217 * @see #setFormat 1218 * @see PixelFormat 1219 */ setDefaultWindowFormat(int format)1220 protected void setDefaultWindowFormat(int format) { 1221 mDefaultWindowFormat = format; 1222 if (!mHaveWindowFormat) { 1223 final WindowManager.LayoutParams attrs = getAttributes(); 1224 attrs.format = format; 1225 if (mCallback != null) { 1226 mCallback.onWindowAttributesChanged(attrs); 1227 } 1228 } 1229 } 1230 1231 /** @hide */ haveDimAmount()1232 protected boolean haveDimAmount() { 1233 return mHaveDimAmount; 1234 } 1235 setChildDrawable(int featureId, Drawable drawable)1236 public abstract void setChildDrawable(int featureId, Drawable drawable); 1237 setChildInt(int featureId, int value)1238 public abstract void setChildInt(int featureId, int value); 1239 1240 /** 1241 * Is a keypress one of the defined shortcut keys for this window. 1242 * @param keyCode the key code from {@link android.view.KeyEvent} to check. 1243 * @param event the {@link android.view.KeyEvent} to use to help check. 1244 */ isShortcutKey(int keyCode, KeyEvent event)1245 public abstract boolean isShortcutKey(int keyCode, KeyEvent event); 1246 1247 /** 1248 * @see android.app.Activity#setVolumeControlStream(int) 1249 */ setVolumeControlStream(int streamType)1250 public abstract void setVolumeControlStream(int streamType); 1251 1252 /** 1253 * @see android.app.Activity#getVolumeControlStream() 1254 */ getVolumeControlStream()1255 public abstract int getVolumeControlStream(); 1256 1257 /** 1258 * Set extra options that will influence the UI for this window. 1259 * @param uiOptions Flags specifying extra options for this window. 1260 */ setUiOptions(int uiOptions)1261 public void setUiOptions(int uiOptions) { } 1262 1263 /** 1264 * Set extra options that will influence the UI for this window. 1265 * Only the bits filtered by mask will be modified. 1266 * @param uiOptions Flags specifying extra options for this window. 1267 * @param mask Flags specifying which options should be modified. Others will remain unchanged. 1268 */ setUiOptions(int uiOptions, int mask)1269 public void setUiOptions(int uiOptions, int mask) { } 1270 } 1271