blob: 17bc0e2de1f80b046dc6e6c8031720b65a49f14a [file] [log] [blame]
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001/*
2 * Copyright (C) 2016 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 com.android.server.wm;
18
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070019import android.content.res.Configuration;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080020
21/**
22 * Class that allows the owner/creator of a {@link WindowContainer} to communicate directly with the
23 * container and make changes.
24 * Note that public calls (mostly in sub-classes) into this class are assumed to be originating from
25 * outside the window manager so the window manager lock is held and appropriate permissions are
26 * checked before calls are allowed to proceed.
27 *
28 * Test class: {@link WindowContainerControllerTests}
29 */
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070030class WindowContainerController<E extends WindowContainer, I extends WindowContainerListener>
31 implements ConfigurationContainerListener {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080032
33 final WindowManagerService mService;
34 final RootWindowContainer mRoot;
Wale Ogunwaledb485de2018-10-29 09:47:07 -070035 final WindowManagerGlobalLock mGlobalLock;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080036
37 // The window container this controller owns.
38 E mContainer;
39 // Interface for communicating changes back to the owner.
40 final I mListener;
41
42 WindowContainerController(I listener, WindowManagerService service) {
43 mListener = listener;
44 mService = service;
45 mRoot = mService != null ? mService.mRoot : null;
Wale Ogunwaledb485de2018-10-29 09:47:07 -070046 mGlobalLock = mService != null ? mService.mGlobalLock : null;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080047 }
48
49 void setContainer(E container) {
50 if (mContainer != null && container != null) {
51 throw new IllegalArgumentException("Can't set container=" + container
52 + " for controller=" + this + " Already set to=" + mContainer);
53 }
54 mContainer = container;
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070055 if (mContainer != null && mListener != null) {
56 mListener.registerConfigurationChangeListener(this);
57 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080058 }
59
60 void removeContainer() {
61 // TODO: See if most uses cases should support removeIfPossible here.
62 //mContainer.removeIfPossible();
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070063 if (mContainer == null) {
64 return;
65 }
66
67 mContainer.setController(null);
68 mContainer = null;
69 if (mListener != null) {
70 mListener.unregisterConfigurationChangeListener(this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080071 }
72 }
73
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070074 @Override
Evan Roskydfe3da72018-10-26 17:21:06 -070075 public void onRequestedOverrideConfigurationChanged(Configuration overrideConfiguration) {
Wale Ogunwaledb485de2018-10-29 09:47:07 -070076 synchronized (mGlobalLock) {
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070077 if (mContainer == null) {
78 return;
79 }
Evan Roskydfe3da72018-10-26 17:21:06 -070080 mContainer.onRequestedOverrideConfigurationChanged(overrideConfiguration);
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070081 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080082 }
83}