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