• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 package android.window;
17 
18 import static android.view.WindowManagerImpl.createWindowContextWindowManager;
19 
20 import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.UiContext;
25 import android.content.ComponentCallbacks;
26 import android.content.ComponentCallbacksController;
27 import android.content.Context;
28 import android.content.ContextWrapper;
29 import android.content.res.Configuration;
30 import android.os.Bundle;
31 import android.view.Display;
32 import android.view.WindowManager;
33 
34 import com.android.internal.annotations.VisibleForTesting;
35 
36 import java.lang.ref.Reference;
37 
38 /**
39  * {@link WindowContext} is a context for non-activity windows such as
40  * {@link android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY} windows or system
41  * windows. Its resources and configuration are adjusted to the area of the display that will be
42  * used when a new window is added via {@link android.view.WindowManager#addView}.
43  *
44  * @see Context#createWindowContext(int, Bundle)
45  * @hide
46  */
47 @UiContext
48 public class WindowContext extends ContextWrapper implements WindowProvider {
49     private final WindowManager mWindowManager;
50     @WindowManager.LayoutParams.WindowType
51     private final int mType;
52     @Nullable
53     private final Bundle mOptions;
54     private final ComponentCallbacksController mCallbacksController =
55             new ComponentCallbacksController();
56     private final WindowContextController mController;
57 
58     /**
59      * Default implementation of {@link WindowContext}
60      * <p>
61      * Note that the users should call {@link Context#createWindowContext(Display, int, Bundle)}
62      * to create a {@link WindowContext} instead of using this constructor
63      * </p><p>
64      * Example usage:
65      * <pre class="prettyprint">
66      * Bundle options = new Bundle();
67      * options.put(KEY_ROOT_DISPLAY_AREA_ID, displayAreaInfo.rootDisplayAreaId);
68      * Context windowContext = context.createWindowContext(display, windowType, options);
69      * </pre></p>
70      *
71      * @param base    Base {@link Context} for this new instance.
72      * @param type    Window type to be used with this context.
73      * @param options A bundle used to pass window-related options.
74      * @see DisplayAreaInfo#rootDisplayAreaId
75      */
WindowContext(@onNull Context base, int type, @Nullable Bundle options)76     public WindowContext(@NonNull Context base, int type, @Nullable Bundle options) {
77         super(base);
78 
79         mType = type;
80         mOptions = options;
81         mWindowManager = createWindowContextWindowManager(this);
82         WindowTokenClient token = (WindowTokenClient) getWindowContextToken();
83         mController = new WindowContextController(token);
84 
85         Reference.reachabilityFence(this);
86     }
87 
88     /**
89      * Attaches this {@link WindowContext} to the {@link com.android.server.wm.DisplayArea}
90      * specified by {@code mType}, {@link #getDisplayId() display ID} and {@code mOptions}
91      * to receive configuration changes.
92      */
attachToDisplayArea()93     public void attachToDisplayArea() {
94         mController.attachToDisplayArea(mType, getDisplayId(), mOptions);
95     }
96 
97     @Override
getSystemService(String name)98     public Object getSystemService(String name) {
99         if (WINDOW_SERVICE.equals(name)) {
100             return mWindowManager;
101         }
102         return super.getSystemService(name);
103     }
104 
105     @Override
finalize()106     protected void finalize() throws Throwable {
107         release();
108         super.finalize();
109     }
110 
111     /** Used for test to invoke because we can't invoke finalize directly. */
112     @VisibleForTesting
release()113     public void release() {
114         mController.detachIfNeeded();
115         destroy();
116     }
117 
118     @Override
destroy()119     public void destroy() {
120         try {
121             mCallbacksController.clearCallbacks();
122             // Called to the base ContextImpl to do final clean-up.
123             getBaseContext().destroy();
124         } finally {
125             Reference.reachabilityFence(this);
126         }
127     }
128 
129     @Override
registerComponentCallbacks(@onNull ComponentCallbacks callback)130     public void registerComponentCallbacks(@NonNull ComponentCallbacks callback) {
131         mCallbacksController.registerCallbacks(callback);
132     }
133 
134     @Override
unregisterComponentCallbacks(@onNull ComponentCallbacks callback)135     public void unregisterComponentCallbacks(@NonNull ComponentCallbacks callback) {
136         mCallbacksController.unregisterCallbacks(callback);
137     }
138 
139     /** Dispatch {@link Configuration} to each {@link ComponentCallbacks}. */
140     @VisibleForTesting(visibility = PACKAGE)
dispatchConfigurationChanged(@onNull Configuration newConfig)141     public void dispatchConfigurationChanged(@NonNull Configuration newConfig) {
142         mCallbacksController.dispatchConfigurationChanged(newConfig);
143     }
144 
145     @Override
getWindowType()146     public int getWindowType() {
147         return mType;
148     }
149 
150     @Nullable
151     @Override
getWindowContextOptions()152     public Bundle getWindowContextOptions() {
153         return mOptions;
154     }
155 }
156