summaryrefslogtreecommitdiff
path: root/lottie_loading_layout/src/com/google/android/setupdesign/GlifLoadingLayout.java
blob: 7284f7b5b4aefde8f65bbd649c67a5a2e659d21d (plain)
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
/*
 * Copyright (C) 2021 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 com.google.android.setupdesign;

import static com.google.android.setupcompat.partnerconfig.Util.isNightMode;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.ColorFilter;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.StringDef;
import androidx.annotation.VisibleForTesting;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.airbnb.lottie.LottieProperty;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.value.LottieValueCallback;
import com.airbnb.lottie.value.SimpleLottieValueCallback;
import com.google.android.setupcompat.partnerconfig.PartnerConfig;
import com.google.android.setupcompat.partnerconfig.PartnerConfig.ResourceType;
import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
import com.google.android.setupcompat.partnerconfig.ResourceEntry;
import com.google.android.setupcompat.template.FooterBarMixin;
import com.google.android.setupcompat.util.BuildCompatUtils;
import com.google.android.setupdesign.lottieloadinglayout.R;
import com.google.android.setupdesign.util.LayoutStyler;
import com.google.android.setupdesign.util.LottieAnimationHelper;
import com.google.android.setupdesign.view.IllustrationVideoView;
import java.io.InputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

/**
 * A GLIF themed layout with a {@link com.airbnb.lottie.LottieAnimationView} to showing lottie
 * illustration and a substitute {@link com.google.android.setupdesign.view.IllustrationVideoView}
 * to showing mp4 illustration. {@code app:sudIllustrationType} can also be used to specify one of
 * the set including "default", "account", "connection", "update", and "final_hold". {@code
 * app:sudLottieRes} can assign the json file of Lottie resource.
 */
public class GlifLoadingLayout extends GlifLayout {

  private static final String TAG = "GlifLoadingLayout";
  View inflatedView;

  @VisibleForTesting @IllustrationType String illustrationType = IllustrationType.DEFAULT;
  @VisibleForTesting LottieAnimationConfig animationConfig = LottieAnimationConfig.CONFIG_DEFAULT;

  @VisibleForTesting @RawRes int customLottieResource = 0;

  private AnimatorListener animatorListener;
  private Runnable nextActionRunnable;
  private boolean workFinished;
  @VisibleForTesting public boolean runRunnable;

  @VisibleForTesting
  public List<LottieAnimationFinishListener> animationFinishListeners = new ArrayList<>();

  public GlifLoadingLayout(Context context) {
    this(context, 0, 0);
  }

  public GlifLoadingLayout(Context context, int template) {
    this(context, template, 0);
  }

  public GlifLoadingLayout(Context context, int template, int containerId) {
    super(context, template, containerId);
    init(null, R.attr.sudLayoutTheme);
  }

  public GlifLoadingLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs, R.attr.sudLayoutTheme);
  }

  @TargetApi(VERSION_CODES.HONEYCOMB)
  public GlifLoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs, defStyleAttr);
  }

  private void init(AttributeSet attrs, int defStyleAttr) {
    registerMixin(FooterBarMixin.class, new LoadingFooterBarMixin(this, attrs, defStyleAttr));

    TypedArray a =
        getContext()
            .obtainStyledAttributes(attrs, R.styleable.SudGlifLoadingLayout, defStyleAttr, 0);
    customLottieResource = a.getResourceId(R.styleable.SudGlifLoadingLayout_sudLottieRes, 0);
    String illustrationType = a.getString(R.styleable.SudGlifLoadingLayout_sudIllustrationType);
    a.recycle();

    if (customLottieResource != 0) {
      inflateLottieView();
      ViewGroup container = findContainer(0);
      container.setVisibility(View.VISIBLE);
    } else {
      if (illustrationType != null) {
        setIllustrationType(illustrationType);
      }

      if (BuildCompatUtils.isAtLeastS()) {
        inflateLottieView();
      } else {
        inflateIllustrationStub();
      }
    }

    View view = findManagedViewById(R.id.sud_layout_loading_content);
    if (view != null) {
      if (shouldApplyPartnerResource()) {
        LayoutStyler.applyPartnerCustomizationExtraPaddingStyle(view);
      }
      tryApplyPartnerCustomizationContentPaddingTopStyle(view);
    }

    updateHeaderHeight();
    updateLandscapeMiddleHorizontalSpacing();

    workFinished = false;
    runRunnable = true;

    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      /*
       * add the listener used to log animation end and check whether the
       * work in background finish when repeated.
       */
      animatorListener =
          new AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
              // Do nothing.
            }

            @Override
            public void onAnimationEnd(Animator animation) {
              Log.i(TAG, "Animate enable:" + isAnimateEnable() + ". Animation end.");
            }

            @Override
            public void onAnimationCancel(Animator animation) {
              // Do nothing.
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
              if (workFinished) {
                Log.i(TAG, "Animation repeat but work finished, run the register runnable.");
                finishRunnable(nextActionRunnable);
                workFinished = false;
              }
            }
          };
      lottieAnimationView.addAnimatorListener(animatorListener);
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (inflatedView instanceof LinearLayout) {
      updateContentPadding((LinearLayout) inflatedView);
    }
  }

  private boolean isAnimateEnable() {
    try {
      if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
        return Settings.Global.getFloat(
                getContext().getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE)
            != 0f;
      } else {
        return true;
      }

    } catch (SettingNotFoundException e) {
      return true;
    }
  }

  public void setIllustrationType(@IllustrationType String type) {
    if (customLottieResource != 0) {
      throw new IllegalStateException(
          "custom illustration already applied, should not set illustration.");
    }

    if (!illustrationType.equals(type)) {
      illustrationType = type;
    }

    switch (type) {
      case IllustrationType.ACCOUNT:
        animationConfig = LottieAnimationConfig.CONFIG_ACCOUNT;
        break;

      case IllustrationType.CONNECTION:
        animationConfig = LottieAnimationConfig.CONFIG_CONNECTION;
        break;

      case IllustrationType.UPDATE:
        animationConfig = LottieAnimationConfig.CONFIG_UPDATE;
        break;

      case IllustrationType.FINAL_HOLD:
        animationConfig = LottieAnimationConfig.CONFIG_FINAL_HOLD;
        break;

      default:
        animationConfig = LottieAnimationConfig.CONFIG_DEFAULT;
        break;
    }

    updateAnimationView();
  }

  // TODO: [GlifLoadingLayout] Should add testcase. LottieAnimationView was auto
  // generated not able to mock. So we have no idea how to detected is the api pass to
  // LottiAnimationView correctly.
  public boolean setAnimation(InputStream inputStream, String keyCache) {
    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      lottieAnimationView.setAnimation(inputStream, keyCache);
      return true;
    } else {
      return false;
    }
  }

  public boolean setAnimation(String assetName) {
    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      lottieAnimationView.setAnimation(assetName);
      return true;
    } else {
      return false;
    }
  }

  public boolean setAnimation(@RawRes int rawRes) {
    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      lottieAnimationView.setAnimation(rawRes);
      return true;
    } else {
      return false;
    }
  }

  private void updateAnimationView() {
    if (BuildCompatUtils.isAtLeastS()) {
      setLottieResource();
    } else {
      setIllustrationResource();
    }
  }

  /**
   * Call this when your activity is done and should be closed. The activity will be finished while
   * animation finished.
   */
  public void finish(@NonNull Activity activity) {
    if (activity == null) {
      throw new NullPointerException("activity should not be null");
    }
    registerAnimationFinishRunnable(activity::finish);
  }

  /**
   * Launch a new activity after the animation finished.
   *
   * @param activity The activity which is GlifLoadingLayout attached to.
   * @param intent The intent to start.
   * @param options Additional options for how the Activity should be started. See {@link
   *     android.content.Context#startActivity(Intent, Bundle)} for more details.
   * @param finish Finish the activity after startActivity
   * @see Activity#startActivity(Intent)
   * @see Activity#startActivityForResult
   */
  public void startActivity(
      @NonNull Activity activity,
      @NonNull Intent intent,
      @Nullable Bundle options,
      boolean finish) {
    if (activity == null) {
      throw new NullPointerException("activity should not be null");
    }

    if (intent == null) {
      throw new NullPointerException("intent should not be null");
    }

    registerAnimationFinishRunnable(
        () -> {
          if (options == null || Build.VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {
            activity.startActivity(intent);
          } else {
            activity.startActivity(intent, options);
          }

          if (finish) {
            activity.finish();
          }
        });
  }

  /**
   * Waiting for the animation finished and launch an activity for which you would like a result
   * when it finished.
   *
   * @param activity The activity which the GlifLoadingLayout attached to.
   * @param intent The intent to start.
   * @param requestCode If >= 0, this code will be returned in onActivityResult() when the activity
   *     exits.
   * @param options Additional options for how the Activity should be started.
   * @param finish Finish the activity after startActivityForResult. The onActivityResult might not
   *     be called because the activity already finished.
   *     <p>See {@link android.content.Context#startActivity(Intent, Bundle)}
   *     Context.startActivity(Intent, Bundle)} for more details.
   */
  public void startActivityForResult(
      @NonNull Activity activity,
      @NonNull Intent intent,
      int requestCode,
      @Nullable Bundle options,
      boolean finish) {
    if (activity == null) {
      throw new NullPointerException("activity should not be null");
    }

    if (intent == null) {
      throw new NullPointerException("intent should not be null");
    }

    registerAnimationFinishRunnable(
        () -> {
          if (options == null || Build.VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {
            activity.startActivityForResult(intent, requestCode);
          } else {
            activity.startActivityForResult(intent, requestCode, options);
          }

          if (finish) {
            activity.finish();
          }
        });
  }

  private void updateHeaderHeight() {
    View headerView = findManagedViewById(R.id.sud_header_scroll_view);
    Configuration currentConfig = getResources().getConfiguration();
    if (headerView != null
        && PartnerConfigHelper.get(getContext())
            .isPartnerConfigAvailable(PartnerConfig.CONFIG_LOADING_LAYOUT_HEADER_HEIGHT)
        && currentConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
      float configHeaderHeight =
          PartnerConfigHelper.get(getContext())
              .getDimension(getContext(), PartnerConfig.CONFIG_LOADING_LAYOUT_HEADER_HEIGHT);
      headerView.getLayoutParams().height = (int) configHeaderHeight;
    }
  }

  private void updateContentPadding(LinearLayout linearLayout) {
    int paddingTop = linearLayout.getPaddingTop();
    int paddingLeft = linearLayout.getPaddingLeft();
    int paddingRight = linearLayout.getPaddingRight();
    int paddingBottom = linearLayout.getPaddingBottom();

    if (PartnerConfigHelper.get(getContext())
        .isPartnerConfigAvailable(PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_TOP)) {
      float configPaddingTop =
          PartnerConfigHelper.get(getContext())
              .getDimension(getContext(), PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_TOP);
      if (configPaddingTop >= 0) {
        paddingTop = (int) configPaddingTop;
      }
    }

    if (PartnerConfigHelper.get(getContext())
        .isPartnerConfigAvailable(PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_START)) {
      float configPaddingLeft =
          PartnerConfigHelper.get(getContext())
              .getDimension(getContext(), PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_START);
      if (configPaddingLeft >= 0) {
        paddingLeft = (int) configPaddingLeft;
      }
    }

    if (PartnerConfigHelper.get(getContext())
        .isPartnerConfigAvailable(PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_END)) {
      float configPaddingRight =
          PartnerConfigHelper.get(getContext())
              .getDimension(getContext(), PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_END);
      if (configPaddingRight >= 0) {
        paddingRight = (int) configPaddingRight;
      }
    }

    if (PartnerConfigHelper.get(getContext())
        .isPartnerConfigAvailable(PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_BOTTOM)) {
      float configPaddingBottom =
          PartnerConfigHelper.get(getContext())
              .getDimension(getContext(), PartnerConfig.CONFIG_LOADING_LAYOUT_PADDING_BOTTOM);
      if (configPaddingBottom >= 0) {
        FooterBarMixin footerBarMixin = getMixin(FooterBarMixin.class);
        if (footerBarMixin == null || footerBarMixin.getButtonContainer() == null) {
          paddingBottom = (int) configPaddingBottom;
        } else {
          paddingBottom =
              (int) configPaddingBottom
                  - (int)
                      Math.min(
                          configPaddingBottom,
                          getButtonContainerHeight(footerBarMixin.getButtonContainer()));
        }
      }
    }

    linearLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
  }

  private static int getButtonContainerHeight(View view) {
    view.measure(
        MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), MeasureSpec.EXACTLY),
        MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), MeasureSpec.EXACTLY));
    return view.getMeasuredHeight();
  }

  private void inflateLottieView() {
    final View lottieLayout = peekLottieLayout();
    if (lottieLayout == null) {
      ViewStub viewStub = findManagedViewById(R.id.sud_loading_layout_lottie_stub);
      if (viewStub != null) {
        inflatedView = viewStub.inflate();
        if (inflatedView instanceof LinearLayout) {
          updateContentPadding((LinearLayout) inflatedView);
        }
        setLottieResource();
      }
    }
  }

  private void inflateIllustrationStub() {
    final View progressLayout = peekProgressIllustrationLayout();
    if (progressLayout == null) {
      ViewStub viewStub = findManagedViewById(R.id.sud_loading_layout_illustration_stub);
      if (viewStub != null) {
        inflatedView = viewStub.inflate();
        if (inflatedView instanceof LinearLayout) {
          updateContentPadding((LinearLayout) inflatedView);
        }
        setIllustrationResource();
      }
    }
  }

  private void setLottieResource() {
    LottieAnimationView lottieView = findViewById(R.id.sud_lottie_view);
    if (lottieView == null) {
      Log.w(TAG, "Lottie view not found, skip set resource. Wait for layout inflated.");
      return;
    }
    if (customLottieResource != 0) {
      InputStream inputRaw = getResources().openRawResource(customLottieResource);
      lottieView.setAnimation(inputRaw, null);
      lottieView.playAnimation();
    } else {
      PartnerConfigHelper partnerConfigHelper = PartnerConfigHelper.get(getContext());
      ResourceEntry resourceEntry =
          partnerConfigHelper.getIllustrationResourceEntry(
              getContext(), animationConfig.getLottieConfig());

      if (resourceEntry != null) {
        InputStream inputRaw =
            resourceEntry.getResources().openRawResource(resourceEntry.getResourceId());
        lottieView.setAnimation(inputRaw, null);
        lottieView.playAnimation();
        setLottieLayoutVisibility(View.VISIBLE);
        setIllustrationLayoutVisibility(View.GONE);
        LottieAnimationHelper.get()
            .applyColor(
                getContext(),
                findLottieAnimationView(),
                isNightMode(getResources().getConfiguration())
                    ? animationConfig.getDarkThemeCustomization()
                    : animationConfig.getLightThemeCustomization());
      } else {
        setLottieLayoutVisibility(View.GONE);
        setIllustrationLayoutVisibility(View.VISIBLE);
        inflateIllustrationStub();
      }
    }
  }

  private void setIllustrationLayoutVisibility(int visibility) {
    View illustrationLayout = findViewById(R.id.sud_layout_progress_illustration);
    if (illustrationLayout != null) {
      illustrationLayout.setVisibility(visibility);
    }
  }

  private void setLottieLayoutVisibility(int visibility) {
    View lottieLayout = findViewById(R.id.sud_layout_lottie_illustration);
    if (lottieLayout != null) {
      lottieLayout.setVisibility(visibility);
    }
  }

  @VisibleForTesting
  boolean isLottieLayoutVisible() {
    View lottieLayout = findViewById(R.id.sud_layout_lottie_illustration);
    return lottieLayout != null && lottieLayout.getVisibility() == View.VISIBLE;
  }

  private void setIllustrationResource() {
    View illustrationLayout = findViewById(R.id.sud_layout_progress_illustration);
    if (illustrationLayout == null) {
      Log.i(TAG, "Illustration stub not inflated, skip set resource");
      return;
    }

    IllustrationVideoView illustrationVideoView =
        findManagedViewById(R.id.sud_progress_illustration);
    ProgressBar progressBar = findManagedViewById(R.id.sud_progress_bar);

    PartnerConfigHelper partnerConfigHelper = PartnerConfigHelper.get(getContext());
    ResourceEntry resourceEntry =
        partnerConfigHelper.getIllustrationResourceEntry(
            getContext(), animationConfig.getIllustrationConfig());

    if (resourceEntry != null) {
      progressBar.setVisibility(GONE);
      illustrationVideoView.setVisibility(VISIBLE);
      illustrationVideoView.setVideoResourceEntry(resourceEntry);
    } else {
      progressBar.setVisibility(VISIBLE);
      illustrationVideoView.setVisibility(GONE);
    }
  }

  private LottieAnimationView findLottieAnimationView() {
    return findViewById(R.id.sud_lottie_view);
  }

  private IllustrationVideoView findIllustrationVideoView() {
    return findManagedViewById(R.id.sud_progress_illustration);
  }

  public void playAnimation() {
    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      lottieAnimationView.setRepeatCount(LottieDrawable.INFINITE);
      lottieAnimationView.playAnimation();
    }
  }

  /** Returns whether the layout is waiting for animation finish or not. */
  public boolean isFinishing() {
    LottieAnimationView lottieAnimationView = findLottieAnimationView();
    if (lottieAnimationView != null) {
      return !animationFinishListeners.isEmpty() && lottieAnimationView.getRepeatCount() == 0;
    } else {
      return false;
    }
  }

  @AnimationType
  public int getAnimationType() {
    if (findLottieAnimationView() != null && isLottieLayoutVisible()) {
      return AnimationType.LOTTIE;
    } else if (findIllustrationVideoView() != null) {
      return AnimationType.ILLUSTRATION;
    } else {
      return AnimationType.PROGRESS_BAR;
    }
  }

  // TODO: Should add testcase with mocked LottieAnimationView.
  /** Add an animator listener to {@link LottieAnimationView}. */
  public void addAnimatorListener(Animator.AnimatorListener listener) {
    LottieAnimationView animationView = findLottieAnimationView();
    if (animationView != null) {
      animationView.addAnimatorListener(listener);
    }
  }

  /** Remove the listener from {@link LottieAnimationView}. */
  public void removeAnimatorListener(AnimatorListener listener) {
    LottieAnimationView animationView = findLottieAnimationView();
    if (animationView != null) {
      animationView.removeAnimatorListener(listener);
    }
  }

  /** Remove all {@link AnimatorListener} from {@link LottieAnimationView}. */
  public void removeAllAnimatorListener() {
    LottieAnimationView animationView = findLottieAnimationView();
    if (animationView != null) {
      animationView.removeAllAnimatorListeners();
    }
  }

  /** Add a value callback with property {@link LottieProperty.COLOR_FILTER}. */
  public void addColorCallback(KeyPath keyPath, LottieValueCallback<ColorFilter> callback) {
    LottieAnimationView animationView = findLottieAnimationView();
    if (animationView != null) {
      animationView.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);
    }
  }

  /** Add a simple value callback with property {@link LottieProperty.COLOR_FILTER}. */
  public void addColorCallback(KeyPath keyPath, SimpleLottieValueCallback<ColorFilter> callback) {
    LottieAnimationView animationView = findLottieAnimationView();
    if (animationView != null) {
      animationView.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);
    }
  }

  @Nullable
  private View peekLottieLayout() {
    return findViewById(R.id.sud_layout_lottie_illustration);
  }

  @Nullable
  private View peekProgressIllustrationLayout() {
    return findViewById(R.id.sud_layout_progress_illustration);
  }

  @Override
  protected View onInflateTemplate(LayoutInflater inflater, int template) {
    if (template == 0) {
      boolean useFullScreenIllustration =
          PartnerConfigHelper.get(getContext())
              .getBoolean(
                  getContext(),
                  PartnerConfig.CONFIG_LOADING_LAYOUT_FULL_SCREEN_ILLUSTRATION_ENABLED,
                  false);
      if (useFullScreenIllustration) {
        template = R.layout.sud_glif_fullscreen_loading_template;
      } else {
        template = R.layout.sud_glif_loading_template;
      }
    }
    return inflateTemplate(inflater, R.style.SudThemeGlif_Light, template);
  }

  @Override
  protected ViewGroup findContainer(int containerId) {
    if (containerId == 0) {
      containerId = R.id.sud_layout_content;
    }
    return super.findContainer(containerId);
  }

  /** The progress config used to maps to different animation */
  public enum LottieAnimationConfig {
    CONFIG_DEFAULT(
        PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_DEFAULT,
        PartnerConfig.CONFIG_LOADING_LOTTIE_DEFAULT,
        PartnerConfig.CONFIG_LOTTIE_LIGHT_THEME_CUSTOMIZATION_DEFAULT,
        PartnerConfig.CONFIG_LOTTIE_DARK_THEME_CUSTOMIZATION_DEFAULT),
    CONFIG_ACCOUNT(
        PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_ACCOUNT,
        PartnerConfig.CONFIG_LOADING_LOTTIE_ACCOUNT,
        PartnerConfig.CONFIG_LOTTIE_LIGHT_THEME_CUSTOMIZATION_ACCOUNT,
        PartnerConfig.CONFIG_LOTTIE_DARK_THEME_CUSTOMIZATION_ACCOUNT),
    CONFIG_CONNECTION(
        PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_CONNECTION,
        PartnerConfig.CONFIG_LOADING_LOTTIE_CONNECTION,
        PartnerConfig.CONFIG_LOTTIE_LIGHT_THEME_CUSTOMIZATION_CONNECTION,
        PartnerConfig.CONFIG_LOTTIE_DARK_THEME_CUSTOMIZATION_CONNECTION),
    CONFIG_UPDATE(
        PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_UPDATE,
        PartnerConfig.CONFIG_LOADING_LOTTIE_UPDATE,
        PartnerConfig.CONFIG_LOTTIE_LIGHT_THEME_CUSTOMIZATION_UPDATE,
        PartnerConfig.CONFIG_LOTTIE_DARK_THEME_CUSTOMIZATION_UPDATE),
    CONFIG_FINAL_HOLD(
        PartnerConfig.CONFIG_PROGRESS_ILLUSTRATION_FINAL_HOLD,
        PartnerConfig.CONFIG_LOADING_LOTTIE_FINAL_HOLD,
        PartnerConfig.CONFIG_LOTTIE_LIGHT_THEME_CUSTOMIZATION_FINAL_HOLD,
        PartnerConfig.CONFIG_LOTTIE_DARK_THEME_CUSTOMIZATION_FINAL_HOLD);

    private final PartnerConfig illustrationConfig;
    private final PartnerConfig lottieConfig;
    private final PartnerConfig lightThemeCustomization;
    private final PartnerConfig darkThemeCustomization;

    LottieAnimationConfig(
        PartnerConfig illustrationConfig,
        PartnerConfig lottieConfig,
        PartnerConfig lightThemeCustomization,
        PartnerConfig darkThemeCustomization) {
      if (illustrationConfig.getResourceType() != ResourceType.ILLUSTRATION
          || lottieConfig.getResourceType() != ResourceType.ILLUSTRATION) {
        throw new IllegalArgumentException(
            "Illustration progress only allow illustration resource");
      }
      this.illustrationConfig = illustrationConfig;
      this.lottieConfig = lottieConfig;
      this.lightThemeCustomization = lightThemeCustomization;
      this.darkThemeCustomization = darkThemeCustomization;
    }

    PartnerConfig getIllustrationConfig() {
      return illustrationConfig;
    }

    PartnerConfig getLottieConfig() {
      return lottieConfig;
    }

    PartnerConfig getLightThemeCustomization() {
      return lightThemeCustomization;
    }

    PartnerConfig getDarkThemeCustomization() {
      return darkThemeCustomization;
    }
  }

  /**
   * Register the {@link Runnable} as a callback that will be performed when the animation finished.
   */
  public void registerAnimationFinishRunnable(Runnable runnable) {
    workFinished = true;
    nextActionRunnable = runnable;
    synchronized (this) {
      runRunnable = true;
      animationFinishListeners.add(
          new LottieAnimationFinishListener(this, () -> finishRunnable(runnable)));
    }
  }

  @VisibleForTesting
  public synchronized void finishRunnable(Runnable runnable) {
    // to avoid run the runnable twice.
    if (runRunnable) {
      runnable.run();
    }
    runRunnable = false;
  }

  /** The listener that to indicate the playing status for lottie animation. */
  @VisibleForTesting
  public static class LottieAnimationFinishListener {

    private final Runnable runnable;
    private final GlifLoadingLayout glifLoadingLayout;
    private final LottieAnimationView lottieAnimationView;

    @VisibleForTesting
    AnimatorListener animatorListener =
        new AnimatorListener() {
          @Override
          public void onAnimationStart(Animator animation) {
            // Do nothing.
          }

          @Override
          public void onAnimationEnd(Animator animation) {
            onAnimationFinished();
          }

          @Override
          public void onAnimationCancel(Animator animation) {
            // Do nothing.
          }

          @Override
          public void onAnimationRepeat(Animator animation) {
            // Do nothing.
          }
        };

    @VisibleForTesting
    LottieAnimationFinishListener(GlifLoadingLayout glifLoadingLayout, Runnable runnable) {
      if (runnable == null) {
        throw new NullPointerException("Runnable can not be null");
      }
      this.glifLoadingLayout = glifLoadingLayout;
      this.runnable = runnable;
      this.lottieAnimationView = glifLoadingLayout.findLottieAnimationView();

      boolean shouldAnimationBeFinished =
          PartnerConfigHelper.get(glifLoadingLayout.getContext())
              .getBoolean(
                  glifLoadingLayout.getContext(),
                  PartnerConfig.CONFIG_LOADING_LAYOUT_WAIT_FOR_ANIMATION_FINISHED,
                  true);
      // TODO: add test case for verify the case which isAnimating returns true.
      if (glifLoadingLayout.isLottieLayoutVisible()
          && lottieAnimationView.isAnimating()
          && !isZeroAnimatorDurationScale()
          && shouldAnimationBeFinished) {
        Log.i(TAG, "Register animation finish.");
        lottieAnimationView.addAnimatorListener(animatorListener);
        lottieAnimationView.setRepeatCount(0);
      } else {
        onAnimationFinished();
      }
    }

    @VisibleForTesting
    boolean isZeroAnimatorDurationScale() {
      try {
        if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
          return Settings.Global.getFloat(
                  glifLoadingLayout.getContext().getContentResolver(),
                  Settings.Global.ANIMATOR_DURATION_SCALE)
              == 0f;
        } else {
          return false;
        }

      } catch (SettingNotFoundException e) {
        return false;
      }
    }

    @VisibleForTesting
    public void onAnimationFinished() {
      runnable.run();
      if (lottieAnimationView != null) {
        lottieAnimationView.removeAnimatorListener(animatorListener);
      }
      glifLoadingLayout.animationFinishListeners.remove(this);
    }
  }

  /** Annotates the state for the illustration. */
  @Retention(RetentionPolicy.SOURCE)
  @StringDef({
    IllustrationType.ACCOUNT,
    IllustrationType.CONNECTION,
    IllustrationType.DEFAULT,
    IllustrationType.UPDATE,
    IllustrationType.FINAL_HOLD
  })
  public @interface IllustrationType {
    String DEFAULT = "default";
    String ACCOUNT = "account";
    String CONNECTION = "connection";
    String UPDATE = "update";
    String FINAL_HOLD = "final_hold";
  }

  /** Annotates the type for the illustration. */
  @Retention(RetentionPolicy.SOURCE)
  @IntDef({AnimationType.LOTTIE, AnimationType.ILLUSTRATION, AnimationType.PROGRESS_BAR})
  public @interface AnimationType {
    int LOTTIE = 1;
    int ILLUSTRATION = 2;
    int PROGRESS_BAR = 3;
  }
}