summaryrefslogtreecommitdiff
path: root/android/support/transition/SyncTransitionListener.java
blob: 4d7e02e82184bbeef9b130e7e27e6b8db7f4187b (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
/*
 * Copyright (C) 2016 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.support.transition;

import android.support.annotation.NonNull;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * This {@link Transition.TransitionListener} synchronously waits for the specified callback.
 */
class SyncTransitionListener implements Transition.TransitionListener {

    static final int EVENT_START = 1;
    static final int EVENT_END = 2;
    static final int EVENT_CANCEL = 3;
    static final int EVENT_PAUSE = 4;
    static final int EVENT_RESUME = 5;

    private final int mTargetEvent;
    private CountDownLatch mLatch = new CountDownLatch(1);

    SyncTransitionListener(int event) {
        mTargetEvent = event;
    }

    boolean await() {
        try {
            return mLatch.await(3000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            return false;
        }
    }

    void reset() {
        mLatch = new CountDownLatch(1);
    }

    @Override
    public void onTransitionStart(@NonNull Transition transition) {
        if (mTargetEvent == EVENT_START) {
            mLatch.countDown();
        }
    }

    @Override
    public void onTransitionEnd(@NonNull Transition transition) {
        if (mTargetEvent == EVENT_END) {
            mLatch.countDown();
        }
    }

    @Override
    public void onTransitionCancel(@NonNull Transition transition) {
        if (mTargetEvent == EVENT_CANCEL) {
            mLatch.countDown();
        }
    }

    @Override
    public void onTransitionPause(@NonNull Transition transition) {
        if (mTargetEvent == EVENT_PAUSE) {
            mLatch.countDown();
        }
    }

    @Override
    public void onTransitionResume(@NonNull Transition transition) {
        if (mTargetEvent == EVENT_RESUME) {
            mLatch.countDown();
        }
    }
}