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.app; 17 18 import android.annotation.NonNull; 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.os.Bundle; 22 import android.os.IBinder; 23 import android.view.WindowManagerGlobal; 24 25 import java.lang.ref.WeakReference; 26 27 /** 28 * Client implementation of {@link IWindowToken}. It can receive configuration change callbacks from 29 * server when window token config is updated or when it is moved between displays, and update the 30 * resources associated with this token on the client side. This will make sure that 31 * {@link WindowContext} instances will have updated resources and configuration. 32 * @hide 33 */ 34 public class WindowTokenClient extends IWindowToken.Stub { 35 /** 36 * Attached {@link Context} for this window token to update configuration and resources. 37 * Initialized by {@link #attachContext(WindowContext)}. 38 */ 39 private WeakReference<WindowContext> mContextRef = null; 40 41 private final ResourcesManager mResourcesManager = ResourcesManager.getInstance(); 42 43 /** 44 * Attaches {@code context} to this {@link WindowTokenClient}. Each {@link WindowTokenClient} 45 * can only attach one {@link Context}. 46 * <p>This method must be called before invoking 47 * {@link android.view.IWindowManager#addWindowTokenWithOptions(IBinder, int, int, Bundle, 48 * String)}.<p/> 49 * 50 * @param context context to be attached 51 * @throws IllegalStateException if attached context has already existed. 52 */ attachContext(@onNull WindowContext context)53 void attachContext(@NonNull WindowContext context) { 54 if (mContextRef != null) { 55 throw new IllegalStateException("Context is already attached."); 56 } 57 mContextRef = new WeakReference<>(context); 58 final ContextImpl impl = ContextImpl.getImpl(context); 59 impl.setResources(impl.createWindowContextResources()); 60 } 61 62 @Override onConfigurationChanged(Configuration newConfig, int newDisplayId)63 public void onConfigurationChanged(Configuration newConfig, int newDisplayId) { 64 final Context context = mContextRef.get(); 65 if (context == null) { 66 return; 67 } 68 final int currentDisplayId = context.getDisplayId(); 69 final boolean displayChanged = newDisplayId != currentDisplayId; 70 final Configuration config = context.getResources().getConfiguration(); 71 final boolean configChanged = config.diff(newConfig) != 0; 72 if (displayChanged || configChanged) { 73 // TODO(ag/9789103): update resource manager logic to track non-activity tokens 74 mResourcesManager.updateResourcesForActivity(this, newConfig, newDisplayId, 75 displayChanged); 76 } 77 if (displayChanged) { 78 context.updateDisplay(newDisplayId); 79 } 80 } 81 82 @Override onWindowTokenRemoved()83 public void onWindowTokenRemoved() { 84 final WindowContext context = mContextRef.get(); 85 if (context != null) { 86 context.destroy(); 87 mContextRef.clear(); 88 } 89 // If a secondary display is detached, release all views attached to this token. 90 WindowManagerGlobal.getInstance().closeAll(this, mContextRef.getClass().getName(), 91 "WindowContext"); 92 } 93 } 94