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