1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.window;
import static android.window.ConfigurationHelper.freeTextLayoutCachesIfNeeded;
import static android.window.ConfigurationHelper.isDifferentDisplay;
import static android.window.ConfigurationHelper.shouldUpdateResources;
import android.annotation.AnyThread;
import android.annotation.BinderThread;
import android.annotation.MainThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityThread;
import android.app.IWindowToken;
import android.app.ResourcesManager;
import android.content.Context;
import android.content.res.Configuration;
import android.inputmethodservice.AbstractInputMethodService;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.IWindowManager;
import android.view.WindowManager.LayoutParams.WindowType;
import android.view.WindowManagerGlobal;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.function.pooled.PooledLambda;
import java.lang.ref.WeakReference;
/**
* This class is used to receive {@link Configuration} changes from the associated window manager
* node on the server side, and apply the change to the {@link Context#getResources() associated
* Resources} of the attached {@link Context}. It is also used as
* {@link Context#getWindowContextToken() the token of non-Activity UI Contexts}.
*
* @see WindowContext
* @see android.view.IWindowManager#attachWindowContextToDisplayArea(IBinder, int, int, Bundle)
*
* @hide
*/
public class WindowTokenClient extends IWindowToken.Stub {
private static final String TAG = WindowTokenClient.class.getSimpleName();
/**
* Attached {@link Context} for this window token to update configuration and resources.
* Initialized by {@link #attachContext(Context)}.
*/
private WeakReference<Context> mContextRef = null;
private final ResourcesManager mResourcesManager = ResourcesManager.getInstance();
private IWindowManager mWms;
@GuardedBy("itself")
private final Configuration mConfiguration = new Configuration();
private boolean mShouldDumpConfigForIme;
private boolean mAttachToWindowContainer;
private final Handler mHandler = ActivityThread.currentActivityThread().getHandler();
/**
* Attaches {@code context} to this {@link WindowTokenClient}. Each {@link WindowTokenClient}
* can only attach one {@link Context}.
* <p>This method must be called before invoking
* {@link android.view.IWindowManager#attachWindowContextToDisplayArea(IBinder, int, int,
* Bundle)}.<p/>
*
* @param context context to be attached
* @throws IllegalStateException if attached context has already existed.
*/
public void attachContext(@NonNull Context context) {
if (mContextRef != null) {
throw new IllegalStateException("Context is already attached.");
}
mContextRef = new WeakReference<>(context);
mShouldDumpConfigForIme = Build.IS_DEBUGGABLE
&& context instanceof AbstractInputMethodService;
}
/**
* Attaches this {@link WindowTokenClient} to a {@link com.android.server.wm.DisplayArea}.
*
* @param type The window type of the {@link WindowContext}
* @param displayId The {@link Context#getDisplayId() ID of display} to associate with
* @param options The window context launched option
* @return {@code true} if attaching successfully.
*/
public boolean attachToDisplayArea(@WindowType int type, int displayId,
@Nullable Bundle options) {
try {
final Configuration configuration = getWindowManagerService()
.attachWindowContextToDisplayArea(this, type, displayId, options);
if (configuration == null) {
return false;
}
onConfigurationChanged(configuration, displayId, false /* shouldReportConfigChange */);
mAttachToWindowContainer = true;
return true;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Attaches this {@link WindowTokenClient} to a {@code DisplayContent}.
*
* @param displayId The {@link Context#getDisplayId() ID of display} to associate with
* @return {@code true} if attaching successfully.
*/
public boolean attachToDisplayContent(int displayId) {
final IWindowManager wms = getWindowManagerService();
// #createSystemUiContext may call this method before WindowManagerService is initialized.
if (wms == null) {
return false;
}
try {
final Configuration configuration = wms.attachToDisplayContent(this, displayId);
if (configuration == null) {
return false;
}
onConfigurationChanged(configuration, displayId, false /* shouldReportConfigChange */);
mAttachToWindowContainer = true;
return true;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Attaches this {@link WindowTokenClient} to a {@code windowToken}.
*
* @param windowToken the window token to associated with
*/
public void attachToWindowToken(IBinder windowToken) {
try {
getWindowManagerService().attachWindowContextToWindowToken(this, windowToken);
mAttachToWindowContainer = true;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** Detaches this {@link WindowTokenClient} from associated WindowContainer if there's one. */
public void detachFromWindowContainerIfNeeded() {
if (!mAttachToWindowContainer) {
return;
}
try {
getWindowManagerService().detachWindowContextFromWindowContainer(this);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
private IWindowManager getWindowManagerService() {
if (mWms == null) {
mWms = WindowManagerGlobal.getWindowManagerService();
}
return mWms;
}
/**
* Called when {@link Configuration} updates from the server side receive.
*
* @param newConfig the updated {@link Configuration}
* @param newDisplayId the updated {@link android.view.Display} ID
*/
@BinderThread
@Override
public void onConfigurationChanged(Configuration newConfig, int newDisplayId) {
mHandler.post(PooledLambda.obtainRunnable(this::onConfigurationChanged, newConfig,
newDisplayId, true /* shouldReportConfigChange */).recycleOnUse());
}
// TODO(b/192048581): rewrite this method based on WindowContext and WindowProviderService
// are inherited from WindowProvider.
/**
* Called when {@link Configuration} updates from the server side receive.
*
* Similar to {@link #onConfigurationChanged(Configuration, int)}, but adds a flag to control
* whether to dispatch configuration update or not.
* <p>
* Note that this method must be executed on the main thread if
* {@code shouldReportConfigChange} is {@code true}, which is usually from
* {@link IWindowToken#onConfigurationChanged(Configuration, int)}
* directly, while this method could be run on any thread if it is used to initialize
* Context's {@code Configuration} via {@link #attachToDisplayArea(int, int, Bundle)}
* or {@link #attachToDisplayContent(int)}.
*
* @param shouldReportConfigChange {@code true} to indicate that the {@code Configuration}
* should be dispatched to listeners.
*
*/
@AnyThread
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public void onConfigurationChanged(Configuration newConfig, int newDisplayId,
boolean shouldReportConfigChange) {
final Context context = mContextRef.get();
if (context == null) {
return;
}
final boolean displayChanged;
final boolean shouldUpdateResources;
final int diff;
final Configuration currentConfig;
synchronized (mConfiguration) {
displayChanged = isDifferentDisplay(context.getDisplayId(), newDisplayId);
shouldUpdateResources = shouldUpdateResources(this, mConfiguration,
newConfig, newConfig /* overrideConfig */, displayChanged,
null /* configChanged */);
diff = mConfiguration.diffPublicOnly(newConfig);
currentConfig = mShouldDumpConfigForIme ? new Configuration(mConfiguration) : null;
if (shouldUpdateResources) {
mConfiguration.setTo(newConfig);
}
}
if (!shouldUpdateResources && mShouldDumpConfigForIme) {
Log.d(TAG, "Configuration not dispatch to IME because configuration is up"
+ " to date. Current config=" + context.getResources().getConfiguration()
+ ", reported config=" + currentConfig
+ ", updated config=" + newConfig);
}
if (shouldUpdateResources) {
// TODO(ag/9789103): update resource manager logic to track non-activity tokens
mResourcesManager.updateResourcesForActivity(this, newConfig, newDisplayId);
if (shouldReportConfigChange && context instanceof WindowContext) {
final WindowContext windowContext = (WindowContext) context;
windowContext.dispatchConfigurationChanged(newConfig);
}
if (shouldReportConfigChange && diff != 0
&& context instanceof WindowProviderService) {
final WindowProviderService windowProviderService = (WindowProviderService) context;
windowProviderService.onConfigurationChanged(newConfig);
}
freeTextLayoutCachesIfNeeded(diff);
if (mShouldDumpConfigForIme) {
if (!shouldReportConfigChange) {
Log.d(TAG, "Only apply configuration update to Resources because "
+ "shouldReportConfigChange is false.\n" + Debug.getCallers(5));
} else if (diff == 0) {
Log.d(TAG, "Configuration not dispatch to IME because configuration has no "
+ " public difference with updated config. "
+ " Current config=" + context.getResources().getConfiguration()
+ ", reported config=" + currentConfig
+ ", updated config=" + newConfig);
}
}
}
if (displayChanged) {
context.updateDisplay(newDisplayId);
}
}
@BinderThread
@Override
public void onWindowTokenRemoved() {
mHandler.post(PooledLambda.obtainRunnable(
WindowTokenClient::onWindowTokenRemovedInner, this).recycleOnUse());
}
@MainThread
private void onWindowTokenRemovedInner() {
final Context context = mContextRef.get();
if (context != null) {
context.destroy();
mContextRef.clear();
}
}
}
|