| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import static android.view.InsetsController.ANIMATION_TYPE_NONE; |
| 20 |
import static android.view.InsetsController.AnimationType; |
| 21 |
import static android.view.InsetsController.DEBUG; |
| 22 |
import static android.view.InsetsSourceConsumerProto.HAS_WINDOW_FOCUS; |
| 23 |
import static android.view.InsetsSourceConsumerProto.INTERNAL_INSETS_TYPE; |
| 24 |
import static android.view.InsetsSourceConsumerProto.IS_REQUESTED_VISIBLE; |
| 25 |
import static android.view.InsetsSourceConsumerProto.PENDING_FRAME; |
| 26 |
import static android.view.InsetsSourceConsumerProto.PENDING_VISIBLE_FRAME; |
| 27 |
import static android.view.InsetsSourceConsumerProto.SOURCE_CONTROL; |
| 28 |
import static android.view.InsetsState.ITYPE_IME; |
| 29 |
import static android.view.InsetsState.getDefaultVisibility; |
| 30 |
import static android.view.InsetsState.toPublicType; |
| 31 |
|
| 32 |
import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE; |
| 33 |
|
| 34 |
import android.annotation.IntDef; |
| 35 |
import android.annotation.Nullable; |
| 36 |
import android.graphics.Rect; |
| 37 |
import android.util.ArraySet; |
| 38 |
import android.util.Log; |
| 39 |
import android.util.proto.ProtoOutputStream; |
| 40 |
import android.view.InsetsState.InternalInsetsType; |
| 41 |
import android.view.SurfaceControl.Transaction; |
| 42 |
import android.view.WindowInsets.Type.InsetsType; |
| 43 |
|
| 44 |
import com.android.internal.annotations.VisibleForTesting; |
| 45 |
import com.android.internal.inputmethod.ImeTracing; |
| 46 |
|
| 47 |
import java.lang.annotation.Retention; |
| 48 |
import java.lang.annotation.RetentionPolicy; |
| 49 |
import java.util.Objects; |
| 50 |
import java.util.function.Supplier; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class InsetsSourceConsumer { |
| 57 |
|
| 58 |
@Retention(RetentionPolicy.SOURCE) |
| 59 |
@IntDef(value = {ShowResult.SHOW_IMMEDIATELY, ShowResult.IME_SHOW_DELAYED, ShowResult.IME_SHOW_FAILED}) |
| 60 |
@interface ShowResult { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
int SHOW_IMMEDIATELY = 0; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
int IME_SHOW_DELAYED = 1; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
int IME_SHOW_FAILED = 2; |
| 75 |
} |
| 76 |
|
| 77 |
protected final InsetsController mController; |
| 78 |
protected boolean mRequestedVisible; |
| 79 |
protected final InsetsState mState; |
| 80 |
protected final @InternalInsetsType int mType; |
| 81 |
|
| 82 |
private static final String TAG = "InsetsSourceConsumer"; |
| 83 |
private final Supplier<Transaction> mTransactionSupplier; |
| 84 |
private @Nullable InsetsSourceControl mSourceControl; |
| 85 |
private boolean mHasWindowFocus; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private boolean mHasViewFocusWhenWindowFocusGain; |
| 91 |
private Rect mPendingFrame; |
| 92 |
private Rect mPendingVisibleFrame; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public InsetsSourceConsumer(@InternalInsetsType int type, InsetsState state, |
| 102 |
Supplier<Transaction> transactionSupplier, InsetsController controller) { |
| 103 |
mType = type; |
| 104 |
mState = state; |
| 105 |
mTransactionSupplier = transactionSupplier; |
| 106 |
mController = controller; |
| 107 |
mRequestedVisible = getDefaultVisibility(type); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public boolean setControl(@Nullable InsetsSourceControl control, |
| 120 |
@InsetsType int[] showTypes, @InsetsType int[] hideTypes) { |
| 121 |
if (mType == ITYPE_IME) { |
| 122 |
ImeTracing.getInstance().triggerClientDump("InsetsSourceConsumer#setControl", |
| 123 |
mController.getHost().getInputMethodManager(), null ); |
| 124 |
} |
| 125 |
if (Objects.equals(mSourceControl, control)) { |
| 126 |
if (mSourceControl != null && mSourceControl != control) { |
| 127 |
mSourceControl.release(SurfaceControl::release); |
| 128 |
mSourceControl = control; |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
final InsetsSourceControl lastControl = mSourceControl; |
| 134 |
mSourceControl = control; |
| 135 |
if (control != null) { |
| 136 |
if (DEBUG) Log.d(TAG, String.format("setControl -> %s on %s", |
| 137 |
InsetsState.typeToString(control.getType()), |
| 138 |
mController.getHost().getRootViewTitle())); |
| 139 |
} |
| 140 |
if (mSourceControl == null) { |
| 141 |
|
| 142 |
mController.notifyControlRevoked(this); |
| 143 |
|
| 144 |
|
| 145 |
final InsetsSource source = mState.getSource(mType); |
| 146 |
final boolean serverVisibility = |
| 147 |
mController.getLastDispatchedState().getSourceOrDefaultVisibility(mType); |
| 148 |
if (source.isVisible() != serverVisibility) { |
| 149 |
source.setVisible(serverVisibility); |
| 150 |
mController.notifyVisibilityChanged(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
applyLocalVisibilityOverride(); |
| 155 |
} else { |
| 156 |
final boolean requestedVisible = isRequestedVisibleAwaitingControl(); |
| 157 |
final SurfaceControl oldLeash = lastControl != null ? lastControl.getLeash() : null; |
| 158 |
final SurfaceControl newLeash = control.getLeash(); |
| 159 |
if (newLeash != null && (oldLeash == null || !newLeash.isSameSurface(oldLeash)) |
| 160 |
&& requestedVisible != control.isInitiallyVisible()) { |
| 161 |
|
| 162 |
|
| 163 |
if (DEBUG) Log.d(TAG, String.format("Gaining leash in %s, requestedVisible: %b", |
| 164 |
mController.getHost().getRootViewTitle(), requestedVisible)); |
| 165 |
if (requestedVisible) { |
| 166 |
showTypes[0] |= toPublicType(getType()); |
| 167 |
} else { |
| 168 |
hideTypes[0] |= toPublicType(getType()); |
| 169 |
} |
| 170 |
} else { |
| 171 |
|
| 172 |
|
| 173 |
if (applyLocalVisibilityOverride()) { |
| 174 |
mController.notifyVisibilityChanged(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (mController.getAnimationType(control.getType()) == ANIMATION_TYPE_NONE) { |
| 180 |
applyRequestedVisibilityToControl(); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if (!requestedVisible && lastControl == null) { |
| 185 |
removeSurface(); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if (lastControl != null) { |
| 190 |
lastControl.release(SurfaceControl::release); |
| 191 |
} |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
@VisibleForTesting |
| 196 |
public InsetsSourceControl getControl() { |
| 197 |
return mSourceControl; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
protected boolean isRequestedVisibleAwaitingControl() { |
| 207 |
return isRequestedVisible(); |
| 208 |
} |
| 209 |
|
| 210 |
int getType() { |
| 211 |
return mType; |
| 212 |
} |
| 213 |
|
| 214 |
@VisibleForTesting |
| 215 |
public void show(boolean fromIme) { |
| 216 |
if (DEBUG) Log.d(TAG, String.format("Call show() for type: %s fromIme: %b ", |
| 217 |
InsetsState.typeToString(mType), fromIme)); |
| 218 |
setRequestedVisible(true); |
| 219 |
} |
| 220 |
|
| 221 |
@VisibleForTesting |
| 222 |
public void hide() { |
| 223 |
if (DEBUG) Log.d(TAG, String.format("Call hide for %s on %s", |
| 224 |
InsetsState.typeToString(mType), mController.getHost().getRootViewTitle())); |
| 225 |
setRequestedVisible(false); |
| 226 |
} |
| 227 |
|
| 228 |
void hide(boolean animationFinished, @AnimationType int animationType) { |
| 229 |
hide(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void onWindowFocusGained(boolean hasViewFocus) { |
| 236 |
mHasWindowFocus = true; |
| 237 |
mHasViewFocusWhenWindowFocusGain = hasViewFocus; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void onWindowFocusLost() { |
| 244 |
mHasWindowFocus = false; |
| 245 |
} |
| 246 |
|
| 247 |
boolean hasViewFocusWhenWindowFocusGain() { |
| 248 |
return mHasViewFocusWhenWindowFocusGain; |
| 249 |
} |
| 250 |
|
| 251 |
boolean applyLocalVisibilityOverride() { |
| 252 |
final InsetsSource source = mState.peekSource(mType); |
| 253 |
final boolean isVisible = source != null ? source.isVisible() : getDefaultVisibility(mType); |
| 254 |
final boolean hasControl = mSourceControl != null; |
| 255 |
|
| 256 |
if (mType == ITYPE_IME) { |
| 257 |
ImeTracing.getInstance().triggerClientDump( |
| 258 |
"InsetsSourceConsumer#applyLocalVisibilityOverride", |
| 259 |
mController.getHost().getInputMethodManager(), null ); |
| 260 |
} |
| 261 |
|
| 262 |
updateCompatSysUiVisibility(hasControl, source, isVisible); |
| 263 |
|
| 264 |
|
| 265 |
if (!hasControl) { |
| 266 |
if (DEBUG) Log.d(TAG, "applyLocalVisibilityOverride: No control in " |
| 267 |
+ mController.getHost().getRootViewTitle() |
| 268 |
+ " requestedVisible " + mRequestedVisible); |
| 269 |
return false; |
| 270 |
} |
| 271 |
if (isVisible == mRequestedVisible) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
if (DEBUG) Log.d(TAG, String.format("applyLocalVisibilityOverride: %s requestedVisible: %b", |
| 275 |
mController.getHost().getRootViewTitle(), mRequestedVisible)); |
| 276 |
mState.getSource(mType).setVisible(mRequestedVisible); |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
private void updateCompatSysUiVisibility(boolean hasControl, InsetsSource source, |
| 281 |
boolean visible) { |
| 282 |
final @InsetsType int publicType = InsetsState.toPublicType(mType); |
| 283 |
if (publicType != WindowInsets.Type.statusBars() |
| 284 |
&& publicType != WindowInsets.Type.navigationBars()) { |
| 285 |
|
| 286 |
return; |
| 287 |
} |
| 288 |
final boolean compatVisible; |
| 289 |
if (hasControl) { |
| 290 |
compatVisible = mRequestedVisible; |
| 291 |
} else if (source != null && !source.getFrame().isEmpty()) { |
| 292 |
compatVisible = visible; |
| 293 |
} else { |
| 294 |
final ArraySet<Integer> types = InsetsState.toInternalType(publicType); |
| 295 |
for (int i = types.size() - 1; i >= 0; i--) { |
| 296 |
final InsetsSource s = mState.peekSource(types.valueAt(i)); |
| 297 |
if (s != null && !s.getFrame().isEmpty()) { |
| 298 |
|
| 299 |
|
| 300 |
return; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
compatVisible = mRequestedVisible; |
| 306 |
} |
| 307 |
mController.updateCompatSysUiVisibility(mType, compatVisible, hasControl); |
| 308 |
} |
| 309 |
|
| 310 |
@VisibleForTesting |
| 311 |
public boolean isRequestedVisible() { |
| 312 |
return mRequestedVisible; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
@VisibleForTesting |
| 324 |
public @ShowResult int requestShow(boolean fromController) { |
| 325 |
return ShowResult.SHOW_IMMEDIATELY; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public void onPerceptible(boolean perceptible) { |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
void notifyHidden() { |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public void removeSurface() { |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
@VisibleForTesting(visibility = PACKAGE) |
| 352 |
public void updateSource(InsetsSource newSource, @AnimationType int animationType) { |
| 353 |
InsetsSource source = mState.peekSource(mType); |
| 354 |
if (source == null || animationType == ANIMATION_TYPE_NONE |
| 355 |
|| source.getFrame().equals(newSource.getFrame())) { |
| 356 |
mPendingFrame = null; |
| 357 |
mPendingVisibleFrame = null; |
| 358 |
mState.addSource(newSource); |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
newSource = new InsetsSource(newSource); |
| 365 |
mPendingFrame = new Rect(newSource.getFrame()); |
| 366 |
mPendingVisibleFrame = newSource.getVisibleFrame() != null |
| 367 |
? new Rect(newSource.getVisibleFrame()) |
| 368 |
: null; |
| 369 |
newSource.setFrame(source.getFrame()); |
| 370 |
newSource.setVisibleFrame(source.getVisibleFrame()); |
| 371 |
mState.addSource(newSource); |
| 372 |
if (DEBUG) Log.d(TAG, "updateSource: " + newSource); |
| 373 |
} |
| 374 |
|
| 375 |
@VisibleForTesting(visibility = PACKAGE) |
| 376 |
public boolean notifyAnimationFinished() { |
| 377 |
if (mPendingFrame != null) { |
| 378 |
InsetsSource source = mState.getSource(mType); |
| 379 |
source.setFrame(mPendingFrame); |
| 380 |
source.setVisibleFrame(mPendingVisibleFrame); |
| 381 |
mPendingFrame = null; |
| 382 |
mPendingVisibleFrame = null; |
| 383 |
return true; |
| 384 |
} |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
protected void setRequestedVisible(boolean requestedVisible) { |
| 393 |
if (mRequestedVisible != requestedVisible) { |
| 394 |
mRequestedVisible = requestedVisible; |
| 395 |
mController.onRequestedVisibilityChanged(this); |
| 396 |
if (DEBUG) Log.d(TAG, "setRequestedVisible: " + requestedVisible); |
| 397 |
} |
| 398 |
if (applyLocalVisibilityOverride()) { |
| 399 |
mController.notifyVisibilityChanged(); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
private void applyRequestedVisibilityToControl() { |
| 404 |
if (mSourceControl == null || mSourceControl.getLeash() == null) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
try (Transaction t = mTransactionSupplier.get()) { |
| 409 |
if (DEBUG) Log.d(TAG, "applyRequestedVisibilityToControl: " + mRequestedVisible); |
| 410 |
if (mRequestedVisible) { |
| 411 |
t.show(mSourceControl.getLeash()); |
| 412 |
} else { |
| 413 |
t.hide(mSourceControl.getLeash()); |
| 414 |
} |
| 415 |
|
| 416 |
t.setAlpha(mSourceControl.getLeash(), mRequestedVisible ? 1 : 0); |
| 417 |
t.apply(); |
| 418 |
} |
| 419 |
onPerceptible(mRequestedVisible); |
| 420 |
} |
| 421 |
|
| 422 |
void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 423 |
final long token = proto.start(fieldId); |
| 424 |
proto.write(INTERNAL_INSETS_TYPE, InsetsState.typeToString(mType)); |
| 425 |
proto.write(HAS_WINDOW_FOCUS, mHasWindowFocus); |
| 426 |
proto.write(IS_REQUESTED_VISIBLE, mRequestedVisible); |
| 427 |
if (mSourceControl != null) { |
| 428 |
mSourceControl.dumpDebug(proto, SOURCE_CONTROL); |
| 429 |
} |
| 430 |
if (mPendingFrame != null) { |
| 431 |
mPendingFrame.dumpDebug(proto, PENDING_FRAME); |
| 432 |
} |
| 433 |
if (mPendingVisibleFrame != null) { |
| 434 |
mPendingVisibleFrame.dumpDebug(proto, PENDING_VISIBLE_FRAME); |
| 435 |
} |
| 436 |
proto.end(token); |
| 437 |
} |
| 438 |
} |