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