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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
|
/*
* Copyright (C) 2018 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.view;
import static android.view.InsetsState.ITYPE_IME;
import static android.view.InsetsState.toPublicType;
import static android.view.WindowInsets.Type.all;
import static android.view.WindowInsets.Type.ime;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_APPEARANCE_CONTROLLED;
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_BEHAVIOR_CONTROLLED;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.Insets;
import android.graphics.Rect;
import android.os.Handler;
import android.os.RemoteException;
import android.util.ArraySet;
import android.util.Log;
import android.util.Pair;
import android.util.Property;
import android.util.SparseArray;
import android.view.InputDevice.MotionRange;
import android.view.InsetsSourceConsumer.ShowResult;
import android.view.InsetsState.InternalInsetsType;
import android.view.SurfaceControl.Transaction;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.WindowInsets.Type;
import android.view.WindowInsets.Type.InsetsType;
import android.view.WindowInsetsAnimationCallback.AnimationBounds;
import android.view.WindowInsetsAnimationCallback.InsetsAnimation;
import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
import android.view.animation.Interpolator;
import android.view.animation.PathInterpolator;
import com.android.internal.annotations.VisibleForTesting;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.function.BiFunction;
/**
* Implements {@link WindowInsetsController} on the client.
* @hide
*/
public class InsetsController implements WindowInsetsController, InsetsAnimationControlCallbacks {
private static final int ANIMATION_DURATION_SHOW_MS = 275;
private static final int ANIMATION_DURATION_HIDE_MS = 340;
private static final int PENDING_CONTROL_TIMEOUT_MS = 2000;
public static final Interpolator INTERPOLATOR = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
/**
* Layout mode during insets animation: The views should be laid out as if the changing inset
* types are fully shown. Before starting the animation, {@link View#onApplyWindowInsets} will
* be called as if the changing insets types are shown, which will result in the views being
* laid out as if the insets are fully shown.
*/
public static final int LAYOUT_INSETS_DURING_ANIMATION_SHOWN = 0;
/**
* Layout mode during insets animation: The views should be laid out as if the changing inset
* types are fully hidden. Before starting the animation, {@link View#onApplyWindowInsets} will
* be called as if the changing insets types are hidden, which will result in the views being
* laid out as if the insets are fully hidden.
*/
public static final int LAYOUT_INSETS_DURING_ANIMATION_HIDDEN = 1;
/**
* Determines the behavior of how the views should be laid out during an insets animation that
* is controlled by the application by calling {@link #controlWindowInsetsAnimation}.
* <p>
* When the animation is system-initiated, the layout mode is always chosen such that the
* pre-animation layout will represent the opposite of the starting state, i.e. when insets
* are appearing, {@link #LAYOUT_INSETS_DURING_ANIMATION_SHOWN} will be used. When insets
* are disappearing, {@link #LAYOUT_INSETS_DURING_ANIMATION_HIDDEN} will be used.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {LAYOUT_INSETS_DURING_ANIMATION_SHOWN,
LAYOUT_INSETS_DURING_ANIMATION_HIDDEN})
@interface LayoutInsetsDuringAnimation {
}
/** Not running an animation. */
@VisibleForTesting
public static final int ANIMATION_TYPE_NONE = -1;
/** Running animation will show insets */
@VisibleForTesting
public static final int ANIMATION_TYPE_SHOW = 0;
/** Running animation will hide insets */
@VisibleForTesting
public static final int ANIMATION_TYPE_HIDE = 1;
/** Running animation is controlled by user via {@link #controlWindowInsetsAnimation} */
@VisibleForTesting
public static final int ANIMATION_TYPE_USER = 2;
@Retention(RetentionPolicy.SOURCE)
@IntDef(value = {ANIMATION_TYPE_NONE, ANIMATION_TYPE_SHOW, ANIMATION_TYPE_HIDE,
ANIMATION_TYPE_USER})
@interface AnimationType {
}
/**
* Translation animation evaluator.
*/
private static TypeEvaluator<Insets> sEvaluator = (fraction, startValue, endValue) -> Insets.of(
(int) (startValue.left + fraction * (endValue.left - startValue.left)),
(int) (startValue.top + fraction * (endValue.top - startValue.top)),
(int) (startValue.right + fraction * (endValue.right - startValue.right)),
(int) (startValue.bottom + fraction * (endValue.bottom - startValue.bottom)));
/**
* Linear animation property
*/
private static class InsetsProperty extends Property<WindowInsetsAnimationController, Insets> {
InsetsProperty() {
super(Insets.class, "Insets");
}
@Override
public Insets get(WindowInsetsAnimationController object) {
return object.getCurrentInsets();
}
@Override
public void set(WindowInsetsAnimationController controller, Insets value) {
controller.setInsetsAndAlpha(
value, 1f /* alpha */, (((InternalAnimationControlListener)
((InsetsAnimationControlImpl) controller).getListener())
.getRawFraction()));
}
}
/**
* The default implementation of listener, to be used by InsetsController and InsetsPolicy to
* animate insets.
*/
public static class InternalAnimationControlListener
implements WindowInsetsAnimationControlListener {
private WindowInsetsAnimationController mController;
private ObjectAnimator mAnimator;
protected boolean mShow;
public InternalAnimationControlListener(boolean show) {
mShow = show;
}
@Override
public void onReady(WindowInsetsAnimationController controller, int types) {
mController = controller;
mAnimator = ObjectAnimator.ofObject(
controller,
new InsetsProperty(),
sEvaluator,
mShow ? controller.getHiddenStateInsets() : controller.getShownStateInsets(),
mShow ? controller.getShownStateInsets() : controller.getHiddenStateInsets()
);
mAnimator.setDuration(getDurationMs());
mAnimator.setInterpolator(INTERPOLATOR);
mAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
onAnimationFinish();
}
});
mAnimator.start();
}
@Override
public void onCancelled() {
// Animator can be null when it is cancelled before onReady() completes.
if (mAnimator != null) {
mAnimator.cancel();
}
}
protected void onAnimationFinish() {
mController.finish(mShow);
}
protected float getRawFraction() {
return (float) mAnimator.getCurrentPlayTime() / mAnimator.getDuration();
}
/**
* To get the animation duration in MS.
*/
public long getDurationMs() {
if (mAnimator != null) {
return mAnimator.getDuration();
}
return mShow ? ANIMATION_DURATION_SHOW_MS : ANIMATION_DURATION_HIDE_MS;
}
}
/**
* Represents a running animation
*/
private static class RunningAnimation {
RunningAnimation(InsetsAnimationControlImpl control, int type) {
this.control = control;
this.type = type;
}
final InsetsAnimationControlImpl control;
final @AnimationType int type;
}
/**
* Represents a control request that we had to defer because we are waiting for the IME to
* process our show request.
*/
private static class PendingControlRequest {
PendingControlRequest(@InsetsType int types, WindowInsetsAnimationControlListener listener,
long durationMs, Interpolator interpolator, @AnimationType int animationType,
@LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation) {
this.types = types;
this.listener = listener;
this.durationMs = durationMs;
this.interpolator = interpolator;
this.animationType = animationType;
this.layoutInsetsDuringAnimation = layoutInsetsDuringAnimation;
}
final @InsetsType int types;
final WindowInsetsAnimationControlListener listener;
final long durationMs;
final Interpolator interpolator;
final @AnimationType int animationType;
final @LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation;
}
private final String TAG = "InsetsControllerImpl";
private final InsetsState mState = new InsetsState();
private final InsetsState mLastDispachedState = new InsetsState();
private final Rect mFrame = new Rect();
private final BiFunction<InsetsController, Integer, InsetsSourceConsumer> mConsumerCreator;
private final SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>();
private final ViewRootImpl mViewRoot;
private final Handler mHandler;
private final SparseArray<InsetsSourceControl> mTmpControlArray = new SparseArray<>();
private final ArrayList<RunningAnimation> mRunningAnimations = new ArrayList<>();
private final ArrayList<InsetsAnimationControlImpl> mTmpFinishedControls = new ArrayList<>();
private WindowInsets mLastInsets;
private boolean mAnimCallbackScheduled;
private final Runnable mAnimCallback;
private final Rect mLastLegacyContentInsets = new Rect();
private final Rect mLastLegacyStableInsets = new Rect();
/** Pending control request that is waiting on IME to be ready to be shown */
private PendingControlRequest mPendingImeControlRequest;
private int mLastLegacySoftInputMode;
private int mLastLegacySystemUiFlags;
private boolean mStartingAnimation;
private SyncRtSurfaceTransactionApplier mApplier;
private Runnable mPendingControlTimeout = this::abortPendingImeControlRequest;
public InsetsController(ViewRootImpl viewRoot) {
this(viewRoot, (controller, type) -> {
if (type == ITYPE_IME) {
return new ImeInsetsSourceConsumer(controller.mState, Transaction::new, controller);
} else {
return new InsetsSourceConsumer(type, controller.mState, Transaction::new,
controller);
}
}, viewRoot.mHandler);
}
@VisibleForTesting
public InsetsController(ViewRootImpl viewRoot,
BiFunction<InsetsController, Integer, InsetsSourceConsumer> consumerCreator,
Handler handler) {
mViewRoot = viewRoot;
mConsumerCreator = consumerCreator;
mHandler = handler;
mAnimCallback = () -> {
mAnimCallbackScheduled = false;
if (mRunningAnimations.isEmpty()) {
return;
}
if (mViewRoot.mView == null) {
// The view has already detached from window.
return;
}
mTmpFinishedControls.clear();
InsetsState state = new InsetsState(mState, true /* copySources */);
for (int i = mRunningAnimations.size() - 1; i >= 0; i--) {
InsetsAnimationControlImpl control = mRunningAnimations.get(i).control;
if (control.applyChangeInsets(state)) {
mTmpFinishedControls.add(control);
}
}
WindowInsets insets = state.calculateInsets(mFrame, mLastInsets.isRound(),
mLastInsets.shouldAlwaysConsumeSystemBars(), mLastInsets.getDisplayCutout(),
mLastLegacyContentInsets, mLastLegacyStableInsets, mLastLegacySoftInputMode,
mLastLegacySystemUiFlags, null /* typeSideMap */);
mViewRoot.mView.dispatchWindowInsetsAnimationProgress(insets);
for (int i = mTmpFinishedControls.size() - 1; i >= 0; i--) {
dispatchAnimationFinished(mTmpFinishedControls.get(i).getAnimation());
}
};
}
@VisibleForTesting
public void onFrameChanged(Rect frame) {
if (mFrame.equals(frame)) {
return;
}
mViewRoot.notifyInsetsChanged();
mFrame.set(frame);
}
@Override
public InsetsState getState() {
return mState;
}
public InsetsState getLastDispatchedState() {
return mLastDispachedState;
}
@VisibleForTesting
public boolean onStateChanged(InsetsState state) {
if (mState.equals(state) && mLastDispachedState.equals(state)) {
return false;
}
mState.set(state);
mLastDispachedState.set(state, true /* copySources */);
applyLocalVisibilityOverride();
mViewRoot.notifyInsetsChanged();
if (!mState.equals(mLastDispachedState)) {
sendStateToWindowManager();
}
return true;
}
/**
* @see InsetsState#calculateInsets
*/
@VisibleForTesting
public WindowInsets calculateInsets(boolean isScreenRound,
boolean alwaysConsumeSystemBars, DisplayCutout cutout, Rect legacyContentInsets,
Rect legacyStableInsets, int legacySoftInputMode, int legacySystemUiFlags) {
mLastLegacyContentInsets.set(legacyContentInsets);
mLastLegacyStableInsets.set(legacyStableInsets);
mLastLegacySoftInputMode = legacySoftInputMode;
mLastLegacySystemUiFlags = legacySystemUiFlags;
mLastInsets = mState.calculateInsets(mFrame, isScreenRound, alwaysConsumeSystemBars, cutout,
legacyContentInsets, legacyStableInsets, legacySoftInputMode, legacySystemUiFlags,
null /* typeSideMap */);
return mLastInsets;
}
/**
* @see InsetsState#calculateVisibleInsets(Rect, Rect, int)
*/
public Rect calculateVisibleInsets(Rect legacyVisibleInsets,
@SoftInputModeFlags int softInputMode) {
return mState.calculateVisibleInsets(mFrame, legacyVisibleInsets, softInputMode);
}
/**
* Called when the server has dispatched us a new set of inset controls.
*/
public void onControlsChanged(InsetsSourceControl[] activeControls) {
if (activeControls != null) {
for (InsetsSourceControl activeControl : activeControls) {
if (activeControl != null) {
// TODO(b/122982984): Figure out why it can be null.
mTmpControlArray.put(activeControl.getType(), activeControl);
}
}
}
int[] showTypes = new int[1];
int[] hideTypes = new int[1];
// Ensure to update all existing source consumers
for (int i = mSourceConsumers.size() - 1; i >= 0; i--) {
final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i);
final InsetsSourceControl control = mTmpControlArray.get(consumer.getType());
// control may be null, but we still need to update the control to null if it got
// revoked.
consumer.setControl(control, showTypes, hideTypes);
}
// Ensure to create source consumers if not available yet.
for (int i = mTmpControlArray.size() - 1; i >= 0; i--) {
final InsetsSourceControl control = mTmpControlArray.valueAt(i);
InsetsSourceConsumer consumer = getSourceConsumer(control.getType());
consumer.setControl(control, showTypes, hideTypes);
}
mTmpControlArray.clear();
if (showTypes[0] != 0) {
applyAnimation(showTypes[0], true /* show */, false /* fromIme */);
}
if (hideTypes[0] != 0) {
applyAnimation(hideTypes[0], false /* show */, false /* fromIme */);
}
}
@Override
public void show(@InsetsType int types) {
show(types, false /* fromIme */);
}
@VisibleForTesting
public void show(@InsetsType int types, boolean fromIme) {
// Handle pending request ready in case there was one set.
if (fromIme && mPendingImeControlRequest != null) {
PendingControlRequest pendingRequest = mPendingImeControlRequest;
mPendingImeControlRequest = null;
mHandler.removeCallbacks(mPendingControlTimeout);
controlAnimationUnchecked(pendingRequest.types, pendingRequest.listener, mFrame,
true /* fromIme */, pendingRequest.durationMs, pendingRequest.interpolator,
false /* fade */, pendingRequest.animationType,
pendingRequest.layoutInsetsDuringAnimation);
return;
}
// TODO: Support a ResultReceiver for IME.
// TODO(b/123718661): Make show() work for multi-session IME.
int typesReady = 0;
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
for (int i = internalTypes.size() - 1; i >= 0; i--) {
@InternalInsetsType int internalType = internalTypes.valueAt(i);
@AnimationType int animationType = getAnimationType(internalType);
InsetsSourceConsumer consumer = getSourceConsumer(internalType);
if (consumer.isRequestedVisible() && animationType == ANIMATION_TYPE_NONE
|| animationType == ANIMATION_TYPE_SHOW) {
// no-op: already shown or animating in (because window visibility is
// applied before starting animation).
continue;
}
typesReady |= InsetsState.toPublicType(consumer.getType());
}
applyAnimation(typesReady, true /* show */, fromIme);
}
@Override
public void hide(@InsetsType int types) {
hide(types, false /* fromIme */);
}
void hide(@InsetsType int types, boolean fromIme) {
int typesReady = 0;
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
for (int i = internalTypes.size() - 1; i >= 0; i--) {
@InternalInsetsType int internalType = internalTypes.valueAt(i);
@AnimationType int animationType = getAnimationType(internalType);
InsetsSourceConsumer consumer = getSourceConsumer(internalType);
if (!consumer.isRequestedVisible() && animationType == ANIMATION_TYPE_NONE
|| animationType == ANIMATION_TYPE_HIDE) {
// no-op: already hidden or animating out.
continue;
}
typesReady |= InsetsState.toPublicType(consumer.getType());
}
applyAnimation(typesReady, false /* show */, fromIme /* fromIme */);
}
@Override
public void controlWindowInsetsAnimation(@InsetsType int types, long durationMs,
@Nullable Interpolator interpolator,
@NonNull WindowInsetsAnimationControlListener listener) {
controlWindowInsetsAnimation(types, listener, false /* fromIme */, durationMs, interpolator,
ANIMATION_TYPE_USER);
}
private void controlWindowInsetsAnimation(@InsetsType int types,
WindowInsetsAnimationControlListener listener, boolean fromIme, long durationMs,
@Nullable Interpolator interpolator, @AnimationType int animationType) {
// If the frame of our window doesn't span the entire display, the control API makes very
// little sense, as we don't deal with negative insets. So just cancel immediately.
if (!mState.getDisplayFrame().equals(mFrame)) {
listener.onCancelled();
return;
}
controlAnimationUnchecked(types, listener, mFrame, fromIme, durationMs, interpolator,
false /* fade */, animationType, getLayoutInsetsDuringAnimationMode(types));
}
private void controlAnimationUnchecked(@InsetsType int types,
WindowInsetsAnimationControlListener listener, Rect frame, boolean fromIme,
long durationMs, Interpolator interpolator, boolean fade,
@AnimationType int animationType,
@LayoutInsetsDuringAnimation int layoutInsetsDuringAnimation) {
if (types == 0) {
// nothing to animate.
listener.onCancelled();
return;
}
cancelExistingControllers(types);
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
final SparseArray<InsetsSourceControl> controls = new SparseArray<>();
Pair<Integer, Boolean> typesReadyPair = collectSourceControls(
fromIme, internalTypes, controls, animationType);
int typesReady = typesReadyPair.first;
boolean imeReady = typesReadyPair.second;
if (!imeReady) {
// IME isn't ready, all requested types will be animated once IME is ready
abortPendingImeControlRequest();
mPendingImeControlRequest = new PendingControlRequest(types, listener, durationMs,
interpolator, animationType, layoutInsetsDuringAnimation);
mHandler.postDelayed(mPendingControlTimeout, PENDING_CONTROL_TIMEOUT_MS);
return;
}
if (typesReady == 0) {
listener.onCancelled();
return;
}
final InsetsAnimationControlImpl controller = new InsetsAnimationControlImpl(controls,
frame, mState, listener, typesReady, this, durationMs, interpolator, fade,
layoutInsetsDuringAnimation);
mRunningAnimations.add(new RunningAnimation(controller, animationType));
}
/**
* @return Pair of (types ready to animate, IME ready to animate).
*/
private Pair<Integer, Boolean> collectSourceControls(boolean fromIme,
ArraySet<Integer> internalTypes, SparseArray<InsetsSourceControl> controls,
@AnimationType int animationType) {
int typesReady = 0;
boolean imeReady = true;
for (int i = internalTypes.size() - 1; i >= 0; i--) {
InsetsSourceConsumer consumer = getSourceConsumer(internalTypes.valueAt(i));
boolean show = animationType == ANIMATION_TYPE_SHOW
|| animationType == ANIMATION_TYPE_USER;
boolean canRun = false;
if (show) {
// Show request
switch(consumer.requestShow(fromIme)) {
case ShowResult.SHOW_IMMEDIATELY:
canRun = true;
break;
case ShowResult.IME_SHOW_DELAYED:
imeReady = false;
break;
case ShowResult.IME_SHOW_FAILED:
// IME cannot be shown (since it didn't have focus), proceed
// with animation of other types.
break;
}
} else {
// Hide request
// TODO: Move notifyHidden() to beginning of the hide animation
// (when visibility actually changes using hideDirectly()).
if (!fromIme) {
consumer.notifyHidden();
}
canRun = true;
}
if (!canRun) {
continue;
}
final InsetsSourceControl control = consumer.getControl();
if (control != null) {
controls.put(consumer.getType(), control);
typesReady |= toPublicType(consumer.getType());
} else if (animationType == ANIMATION_TYPE_SHOW) {
// We don't have a control at the moment. However, we still want to update requested
// visibility state such that in case we get control, we can apply show animation.
consumer.show(fromIme);
} else if (animationType == ANIMATION_TYPE_HIDE) {
consumer.hide();
}
}
return new Pair<>(typesReady, imeReady);
}
private @LayoutInsetsDuringAnimation int getLayoutInsetsDuringAnimationMode(
@InsetsType int types) {
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
// Generally, we want to layout the opposite of the current state. This is to make animation
// callbacks easy to use: The can capture the layout values and then treat that as end-state
// during the animation.
//
// However, if controlling multiple sources, we want to treat it as shown if any of the
// types is currently hidden.
for (int i = internalTypes.size() - 1; i >= 0; i--) {
InsetsSourceConsumer consumer = mSourceConsumers.get(internalTypes.valueAt(i));
if (consumer == null) {
continue;
}
if (!consumer.isRequestedVisible()) {
return LAYOUT_INSETS_DURING_ANIMATION_SHOWN;
}
}
return LAYOUT_INSETS_DURING_ANIMATION_HIDDEN;
}
private void cancelExistingControllers(@InsetsType int types) {
for (int i = mRunningAnimations.size() - 1; i >= 0; i--) {
InsetsAnimationControlImpl control = mRunningAnimations.get(i).control;
if ((control.getTypes() & types) != 0) {
cancelAnimation(control, true /* invokeCallback */);
}
}
if ((types & ime()) != 0) {
abortPendingImeControlRequest();
}
}
private void abortPendingImeControlRequest() {
if (mPendingImeControlRequest != null) {
mPendingImeControlRequest.listener.onCancelled();
mPendingImeControlRequest = null;
mHandler.removeCallbacks(mPendingControlTimeout);
}
}
@VisibleForTesting
@Override
public void notifyFinished(InsetsAnimationControlImpl controller, boolean shown) {
cancelAnimation(controller, false /* invokeCallback */);
if (shown) {
showDirectly(controller.getTypes());
} else {
hideDirectly(controller.getTypes());
}
}
@Override
public void applySurfaceParams(final SyncRtSurfaceTransactionApplier.SurfaceParams... params) {
if (mApplier == null) {
if (mViewRoot.mView == null) {
throw new IllegalStateException("View of the ViewRootImpl is not initiated.");
}
mApplier = new SyncRtSurfaceTransactionApplier(mViewRoot.mView);
}
mApplier.scheduleApply(params);
}
void notifyControlRevoked(InsetsSourceConsumer consumer) {
for (int i = mRunningAnimations.size() - 1; i >= 0; i--) {
InsetsAnimationControlImpl control = mRunningAnimations.get(i).control;
if ((control.getTypes() & toPublicType(consumer.getType())) != 0) {
cancelAnimation(control, true /* invokeCallback */);
}
}
if (consumer.getType() == ITYPE_IME) {
abortPendingImeControlRequest();
}
}
private void cancelAnimation(InsetsAnimationControlImpl control, boolean invokeCallback) {
if (invokeCallback) {
control.onCancelled();
}
for (int i = mRunningAnimations.size() - 1; i >= 0; i--) {
if (mRunningAnimations.get(i).control == control) {
mRunningAnimations.remove(i);
break;
}
}
}
private void applyLocalVisibilityOverride() {
for (int i = mSourceConsumers.size() - 1; i >= 0; i--) {
final InsetsSourceConsumer controller = mSourceConsumers.valueAt(i);
controller.applyLocalVisibilityOverride();
}
}
@VisibleForTesting
public @NonNull InsetsSourceConsumer getSourceConsumer(@InternalInsetsType int type) {
InsetsSourceConsumer controller = mSourceConsumers.get(type);
if (controller != null) {
return controller;
}
controller = mConsumerCreator.apply(this, type);
mSourceConsumers.put(type, controller);
return controller;
}
@VisibleForTesting
public void notifyVisibilityChanged() {
mViewRoot.notifyInsetsChanged();
sendStateToWindowManager();
}
/**
* @see ViewRootImpl#updateCompatSysUiVisibility(int, boolean, boolean)
*/
public void updateCompatSysUiVisibility(@InternalInsetsType int type, boolean visible,
boolean hasControl) {
mViewRoot.updateCompatSysUiVisibility(type, visible, hasControl);
}
/**
* Called when current window gains focus.
*/
public void onWindowFocusGained() {
getSourceConsumer(ITYPE_IME).onWindowFocusGained();
}
/**
* Called when current window loses focus.
*/
public void onWindowFocusLost() {
getSourceConsumer(ITYPE_IME).onWindowFocusLost();
}
ViewRootImpl getViewRoot() {
return mViewRoot;
}
/**
* Used by {@link ImeInsetsSourceConsumer} when IME decides to be shown/hidden.
* @hide
*/
@VisibleForTesting
public void applyImeVisibility(boolean setVisible) {
if (setVisible) {
show(Type.IME, true /* fromIme */);
} else {
hide(Type.IME);
}
}
@VisibleForTesting
public @AnimationType int getAnimationType(@InternalInsetsType int type) {
for (int i = mRunningAnimations.size() - 1; i >= 0; i--) {
InsetsAnimationControlImpl control = mRunningAnimations.get(i).control;
if (control.controlsInternalType(type)) {
return mRunningAnimations.get(i).type;
}
}
return ANIMATION_TYPE_NONE;
}
/**
* Sends the local visibility state back to window manager.
*/
private void sendStateToWindowManager() {
InsetsState tmpState = new InsetsState();
for (int i = mSourceConsumers.size() - 1; i >= 0; i--) {
final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i);
if (consumer.getControl() != null) {
tmpState.addSource(mState.getSource(consumer.getType()));
}
}
// TODO: Put this on a dispatcher thread.
try {
mViewRoot.mWindowSession.insetsModified(mViewRoot.mWindow, tmpState);
} catch (RemoteException e) {
Log.e(TAG, "Failed to call insetsModified", e);
}
}
private void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) {
if (types == 0) {
// nothing to animate.
return;
}
final InternalAnimationControlListener listener =
new InternalAnimationControlListener(show);
// Show/hide animations always need to be relative to the display frame, in order that shown
// and hidden state insets are correct.
controlAnimationUnchecked(
types, listener, mState.getDisplayFrame(), fromIme, listener.getDurationMs(),
INTERPOLATOR, true /* fade */, show ? ANIMATION_TYPE_SHOW : ANIMATION_TYPE_HIDE,
show ? LAYOUT_INSETS_DURING_ANIMATION_SHOWN
: LAYOUT_INSETS_DURING_ANIMATION_HIDDEN);
}
private void hideDirectly(@InsetsType int types) {
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
for (int i = internalTypes.size() - 1; i >= 0; i--) {
getSourceConsumer(internalTypes.valueAt(i)).hide();
}
}
private void showDirectly(@InsetsType int types) {
final ArraySet<Integer> internalTypes = InsetsState.toInternalType(types);
for (int i = internalTypes.size() - 1; i >= 0; i--) {
getSourceConsumer(internalTypes.valueAt(i)).show(false /* fromIme */);
}
}
/**
* Cancel on-going animation to show/hide {@link InsetsType}.
*/
@VisibleForTesting
public void cancelExistingAnimation() {
cancelExistingControllers(all());
}
void dump(String prefix, PrintWriter pw) {
pw.println(prefix); pw.println("InsetsController:");
mState.dump(prefix + " ", pw);
}
@VisibleForTesting
@Override
public void startAnimation(InsetsAnimationControlImpl controller,
WindowInsetsAnimationControlListener listener, int types, InsetsAnimation animation,
AnimationBounds bounds, int layoutDuringAnimation) {
if (layoutDuringAnimation == LAYOUT_INSETS_DURING_ANIMATION_SHOWN) {
showDirectly(types);
} else {
hideDirectly(types);
}
mViewRoot.mView.dispatchWindowInsetsAnimationPrepare(animation);
mViewRoot.mView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mViewRoot.mView.getViewTreeObserver().removeOnPreDrawListener(this);
if (controller.isCancelled()) {
return true;
}
mViewRoot.mView.dispatchWindowInsetsAnimationStart(animation, bounds);
mStartingAnimation = true;
listener.onReady(controller, types);
mStartingAnimation = false;
return true;
}
});
mViewRoot.mView.invalidate();
}
@VisibleForTesting
public void dispatchAnimationFinished(InsetsAnimation animation) {
mViewRoot.mView.dispatchWindowInsetsAnimationFinish(animation);
}
@VisibleForTesting
@Override
public void scheduleApplyChangeInsets() {
if (mStartingAnimation) {
mAnimCallback.run();
mAnimCallbackScheduled = false;
return;
}
if (!mAnimCallbackScheduled) {
mViewRoot.mChoreographer.postCallback(Choreographer.CALLBACK_INSETS_ANIMATION,
mAnimCallback, null /* token*/);
mAnimCallbackScheduled = true;
}
}
@Override
public void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask) {
mViewRoot.mWindowAttributes.privateFlags |= PRIVATE_FLAG_APPEARANCE_CONTROLLED;
final InsetsFlags insetsFlags = mViewRoot.mWindowAttributes.insetsFlags;
if (insetsFlags.appearance != appearance) {
insetsFlags.appearance = (insetsFlags.appearance & ~mask) | (appearance & mask);
mViewRoot.mWindowAttributesChanged = true;
mViewRoot.scheduleTraversals();
}
}
@Override
public @Appearance int getSystemBarsAppearance() {
if ((mViewRoot.mWindowAttributes.privateFlags & PRIVATE_FLAG_APPEARANCE_CONTROLLED) == 0) {
// We only return the requested appearance, not the implied one.
return 0;
}
return mViewRoot.mWindowAttributes.insetsFlags.appearance;
}
@Override
public void setSystemBarsBehavior(@Behavior int behavior) {
mViewRoot.mWindowAttributes.privateFlags |= PRIVATE_FLAG_BEHAVIOR_CONTROLLED;
if (mViewRoot.mWindowAttributes.insetsFlags.behavior != behavior) {
mViewRoot.mWindowAttributes.insetsFlags.behavior = behavior;
mViewRoot.mWindowAttributesChanged = true;
mViewRoot.scheduleTraversals();
}
}
@Override
public @Appearance int getSystemBarsBehavior() {
if ((mViewRoot.mWindowAttributes.privateFlags & PRIVATE_FLAG_BEHAVIOR_CONTROLLED) == 0) {
// We only return the requested behavior, not the implied one.
return 0;
}
return mViewRoot.mWindowAttributes.insetsFlags.behavior;
}
}
|