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