| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.window; |
| 17 |
|
| 18 |
import static android.window.ConfigurationHelper.freeTextLayoutCachesIfNeeded; |
| 19 |
import static android.window.ConfigurationHelper.isDifferentDisplay; |
| 20 |
import static android.window.ConfigurationHelper.shouldUpdateResources; |
| 21 |
|
| 22 |
import android.annotation.AnyThread; |
| 23 |
import android.annotation.BinderThread; |
| 24 |
import android.annotation.MainThread; |
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.Nullable; |
| 27 |
import android.app.ActivityThread; |
| 28 |
import android.app.IWindowToken; |
| 29 |
import android.app.ResourcesManager; |
| 30 |
import android.content.Context; |
| 31 |
import android.content.res.Configuration; |
| 32 |
import android.inputmethodservice.AbstractInputMethodService; |
| 33 |
import android.os.Build; |
| 34 |
import android.os.Bundle; |
| 35 |
import android.os.Debug; |
| 36 |
import android.os.Handler; |
| 37 |
import android.os.IBinder; |
| 38 |
import android.os.RemoteException; |
| 39 |
import android.util.Log; |
| 40 |
import android.view.IWindowManager; |
| 41 |
import android.view.WindowManager.LayoutParams.WindowType; |
| 42 |
import android.view.WindowManagerGlobal; |
| 43 |
|
| 44 |
import com.android.internal.annotations.GuardedBy; |
| 45 |
import com.android.internal.annotations.VisibleForTesting; |
| 46 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 47 |
|
| 48 |
import java.lang.ref.WeakReference; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class WindowTokenClient extends IWindowToken.Stub { |
| 62 |
private static final String TAG = WindowTokenClient.class.getSimpleName(); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private WeakReference<Context> mContextRef = null; |
| 69 |
|
| 70 |
private final ResourcesManager mResourcesManager = ResourcesManager.getInstance(); |
| 71 |
|
| 72 |
private IWindowManager mWms; |
| 73 |
|
| 74 |
@GuardedBy("itself") |
| 75 |
private final Configuration mConfiguration = new Configuration(); |
| 76 |
|
| 77 |
private boolean mShouldDumpConfigForIme; |
| 78 |
|
| 79 |
private boolean mAttachToWindowContainer; |
| 80 |
|
| 81 |
private final Handler mHandler = ActivityThread.currentActivityThread().getHandler(); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void attachContext(@NonNull Context context) { |
| 94 |
if (mContextRef != null) { |
| 95 |
throw new IllegalStateException("Context is already attached."); |
| 96 |
} |
| 97 |
mContextRef = new WeakReference<>(context); |
| 98 |
mShouldDumpConfigForIme = Build.IS_DEBUGGABLE |
| 99 |
&& context instanceof AbstractInputMethodService; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public boolean attachToDisplayArea(@WindowType int type, int displayId, |
| 111 |
@Nullable Bundle options) { |
| 112 |
try { |
| 113 |
final Configuration configuration = getWindowManagerService() |
| 114 |
.attachWindowContextToDisplayArea(this, type, displayId, options); |
| 115 |
if (configuration == null) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
onConfigurationChanged(configuration, displayId, false ); |
| 119 |
mAttachToWindowContainer = true; |
| 120 |
return true; |
| 121 |
} catch (RemoteException e) { |
| 122 |
throw e.rethrowFromSystemServer(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public boolean attachToDisplayContent(int displayId) { |
| 133 |
final IWindowManager wms = getWindowManagerService(); |
| 134 |
|
| 135 |
if (wms == null) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
try { |
| 139 |
final Configuration configuration = wms.attachToDisplayContent(this, displayId); |
| 140 |
if (configuration == null) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
onConfigurationChanged(configuration, displayId, false ); |
| 144 |
mAttachToWindowContainer = true; |
| 145 |
return true; |
| 146 |
} catch (RemoteException e) { |
| 147 |
throw e.rethrowFromSystemServer(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void attachToWindowToken(IBinder windowToken) { |
| 157 |
try { |
| 158 |
getWindowManagerService().attachWindowContextToWindowToken(this, windowToken); |
| 159 |
mAttachToWindowContainer = true; |
| 160 |
} catch (RemoteException e) { |
| 161 |
throw e.rethrowFromSystemServer(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
public void detachFromWindowContainerIfNeeded() { |
| 167 |
if (!mAttachToWindowContainer) { |
| 168 |
return; |
| 169 |
} |
| 170 |
try { |
| 171 |
getWindowManagerService().detachWindowContextFromWindowContainer(this); |
| 172 |
} catch (RemoteException e) { |
| 173 |
throw e.rethrowFromSystemServer(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private IWindowManager getWindowManagerService() { |
| 178 |
if (mWms == null) { |
| 179 |
mWms = WindowManagerGlobal.getWindowManagerService(); |
| 180 |
} |
| 181 |
return mWms; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@BinderThread |
| 191 |
@Override |
| 192 |
public void onConfigurationChanged(Configuration newConfig, int newDisplayId) { |
| 193 |
mHandler.post(PooledLambda.obtainRunnable(this::onConfigurationChanged, newConfig, |
| 194 |
newDisplayId, true ).recycleOnUse()); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@AnyThread |
| 217 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 218 |
public void onConfigurationChanged(Configuration newConfig, int newDisplayId, |
| 219 |
boolean shouldReportConfigChange) { |
| 220 |
final Context context = mContextRef.get(); |
| 221 |
if (context == null) { |
| 222 |
return; |
| 223 |
} |
| 224 |
final boolean displayChanged; |
| 225 |
final boolean shouldUpdateResources; |
| 226 |
final int diff; |
| 227 |
final Configuration currentConfig; |
| 228 |
|
| 229 |
synchronized (mConfiguration) { |
| 230 |
displayChanged = isDifferentDisplay(context.getDisplayId(), newDisplayId); |
| 231 |
shouldUpdateResources = shouldUpdateResources(this, mConfiguration, |
| 232 |
newConfig, newConfig , displayChanged, |
| 233 |
null ); |
| 234 |
diff = mConfiguration.diffPublicOnly(newConfig); |
| 235 |
currentConfig = mShouldDumpConfigForIme ? new Configuration(mConfiguration) : null; |
| 236 |
if (shouldUpdateResources) { |
| 237 |
mConfiguration.setTo(newConfig); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (!shouldUpdateResources && mShouldDumpConfigForIme) { |
| 242 |
Log.d(TAG, "Configuration not dispatch to IME because configuration is up" |
| 243 |
+ " to date. Current config=" + context.getResources().getConfiguration() |
| 244 |
+ ", reported config=" + currentConfig |
| 245 |
+ ", updated config=" + newConfig); |
| 246 |
} |
| 247 |
if (shouldUpdateResources) { |
| 248 |
|
| 249 |
mResourcesManager.updateResourcesForActivity(this, newConfig, newDisplayId); |
| 250 |
|
| 251 |
if (shouldReportConfigChange && context instanceof WindowContext) { |
| 252 |
final WindowContext windowContext = (WindowContext) context; |
| 253 |
windowContext.dispatchConfigurationChanged(newConfig); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
if (shouldReportConfigChange && diff != 0 |
| 258 |
&& context instanceof WindowProviderService) { |
| 259 |
final WindowProviderService windowProviderService = (WindowProviderService) context; |
| 260 |
windowProviderService.onConfigurationChanged(newConfig); |
| 261 |
} |
| 262 |
freeTextLayoutCachesIfNeeded(diff); |
| 263 |
if (mShouldDumpConfigForIme) { |
| 264 |
if (!shouldReportConfigChange) { |
| 265 |
Log.d(TAG, "Only apply configuration update to Resources because " |
| 266 |
+ "shouldReportConfigChange is false.\n" + Debug.getCallers(5)); |
| 267 |
} else if (diff == 0) { |
| 268 |
Log.d(TAG, "Configuration not dispatch to IME because configuration has no " |
| 269 |
+ " public difference with updated config. " |
| 270 |
+ " Current config=" + context.getResources().getConfiguration() |
| 271 |
+ ", reported config=" + currentConfig |
| 272 |
+ ", updated config=" + newConfig); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
if (displayChanged) { |
| 277 |
context.updateDisplay(newDisplayId); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
@BinderThread |
| 282 |
@Override |
| 283 |
public void onWindowTokenRemoved() { |
| 284 |
mHandler.post(PooledLambda.obtainRunnable( |
| 285 |
WindowTokenClient::onWindowTokenRemovedInner, this).recycleOnUse()); |
| 286 |
} |
| 287 |
|
| 288 |
@MainThread |
| 289 |
private void onWindowTokenRemovedInner() { |
| 290 |
final Context context = mContextRef.get(); |
| 291 |
if (context != null) { |
| 292 |
context.destroy(); |
| 293 |
mContextRef.clear(); |
| 294 |
} |
| 295 |
} |
| 296 |
} |