WindowManagerImpl.java revision 7c9746d4ef8ab3c500b501ab32933c5172a856ab
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.os.IBinder;
21
22/**
23 * Provides low-level communication with the system window manager for
24 * operations that are bound to a particular context, display or parent window.
25 * Instances of this object are sensitive to the compatibility info associated
26 * with the running application.
27 *
28 * This object implements the {@link ViewManager} interface,
29 * allowing you to add any View subclass as a top-level window on the screen.
30 * Additional window manager specific layout parameters are defined for
31 * control over how windows are displayed.  It also implements the {@link WindowManager}
32 * interface, allowing you to control the displays attached to the device.
33 *
34 * <p>Applications will not normally use WindowManager directly, instead relying
35 * on the higher-level facilities in {@link android.app.Activity} and
36 * {@link android.app.Dialog}.
37 *
38 * <p>Even for low-level window manager access, it is almost never correct to use
39 * this class.  For example, {@link android.app.Activity#getWindowManager}
40 * provides a window manager for adding windows that are associated with that
41 * activity -- the window manager will not normally allow you to add arbitrary
42 * windows that are not associated with an activity.
43 *
44 * @see WindowManager
45 * @see WindowManagerGlobal
46 * @hide
47 */
48public final class WindowManagerImpl implements WindowManager {
49    private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
50    private final Display mDisplay;
51    private final Window mParentWindow;
52
53    private IBinder mDefaultToken;
54
55    public WindowManagerImpl(Display display) {
56        this(display, null);
57    }
58
59    private WindowManagerImpl(Display display, Window parentWindow) {
60        mDisplay = display;
61        mParentWindow = parentWindow;
62    }
63
64    public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
65        return new WindowManagerImpl(mDisplay, parentWindow);
66    }
67
68    public WindowManagerImpl createPresentationWindowManager(Display display) {
69        return new WindowManagerImpl(display, mParentWindow);
70    }
71
72    /**
73     * Sets the window token to assign when none is specified by the client or
74     * available from the parent window.
75     *
76     * @param token The default token to assign.
77     */
78    public void setDefaultToken(IBinder token) {
79        mDefaultToken = token;
80    }
81
82    @Override
83    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
84        applyDefaultToken(params);
85        mGlobal.addView(view, params, mDisplay, mParentWindow);
86    }
87
88    @Override
89    public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
90        applyDefaultToken(params);
91        mGlobal.updateViewLayout(view, params);
92    }
93
94    private void applyDefaultToken(@NonNull ViewGroup.LayoutParams params) {
95        // Only use the default token if we don't have a parent window.
96        if (mDefaultToken != null && mParentWindow == null) {
97            if (!(params instanceof WindowManager.LayoutParams)) {
98                throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
99            }
100
101            // Only use the default token if we don't already have a token.
102            final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
103            if (wparams.token == null) {
104                wparams.token = mDefaultToken;
105            }
106        }
107    }
108
109    @Override
110    public void removeView(View view) {
111        mGlobal.removeView(view, false);
112    }
113
114    @Override
115    public void removeViewImmediate(View view) {
116        mGlobal.removeView(view, true);
117    }
118
119    @Override
120    public Display getDefaultDisplay() {
121        return mDisplay;
122    }
123}
124