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