1 /* 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 17 package com.android.server.wm; 18 19 import android.content.res.Configuration; 20 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 */ 30 class WindowContainerController<E extends WindowContainer, I extends WindowContainerListener> 31 implements ConfigurationContainerListener { 32 33 final WindowManagerService mService; 34 final RootWindowContainer mRoot; 35 final WindowHashMap mWindowMap; 36 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; 46 mWindowMap = mService != null ? mService.mWindowMap : null; 47 } 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; 55 if (mContainer != null && mListener != null) { 56 mListener.registerConfigurationChangeListener(this); 57 } 58 } 59 60 void removeContainer() { 61 // TODO: See if most uses cases should support removeIfPossible here. 62 //mContainer.removeIfPossible(); 63 if (mContainer == null) { 64 return; 65 } 66 67 mContainer.setController(null); 68 mContainer = null; 69 if (mListener != null) { 70 mListener.unregisterConfigurationChangeListener(this); 71 } 72 } 73 74 @Override 75 public void onOverrideConfigurationChanged(Configuration overrideConfiguration) { 76 synchronized (mWindowMap) { 77 if (mContainer == null) { 78 return; 79 } 80 mContainer.onOverrideConfigurationChanged(overrideConfiguration); 81 } 82 } 83 } 84