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