summaryrefslogtreecommitdiffstats
path: root/src/com/android/incallui/CircularRevealActivity.java
blob: 7a9b7ccbfac1eb12d60044c78d96543fd30b432e (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
/*
 * Copyright (C) 2014 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.android.incallui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Outline;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Display;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewOutlineProvider;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;

import com.android.contacts.common.interactions.TouchPointManager;
import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;

/**
 * Lightweight activity used to display a circular reveal while InCallActivity is starting up.
 * A BroadcastReceiver is used to listen to broadcasts from a LocalBroadcastManager to finish
 * the activity at suitable times.
 */
public class CircularRevealActivity extends Activity {
    private static final int REVEAL_DURATION = 333;
    public static final String EXTRA_THEME_COLORS = "extra_theme_colors";
    public static final String ACTION_CLEAR_DISPLAY = "action_clear_display";

    final BroadcastReceiver mClearDisplayReceiver = new BroadcastReceiver( ) {
        @Override
        public void onReceive(Context context, Intent intent) {
            clearDisplay();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(0, 0);
        setContentView(R.layout.outgoing_call_animation);
        final Point touchPoint = getIntent().getParcelableExtra(TouchPointManager.TOUCH_POINT);
        final MaterialPalette palette = getIntent().getParcelableExtra(EXTRA_THEME_COLORS);
        setupDecorView(touchPoint, palette);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!InCallPresenter.getInstance().isServiceBound()) {
            clearDisplay();
        }
        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_CLEAR_DISPLAY);
        LocalBroadcastManager.getInstance(this).registerReceiver(mClearDisplayReceiver, filter);
    }

    @Override
    protected void onStop() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mClearDisplayReceiver);
        super.onStop();
    }

    private void setupDecorView(final Point touchPoint, MaterialPalette palette) {
        final View view  = getWindow().getDecorView();

        // The circle starts from an initial size of 0 so clip it such that it is invisible. When
        // the animation later starts, this clip will be clobbered by the circular reveal clip.
        // See ViewAnimationUtils.createCircularReveal.
        view.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                // Using (0, 0, 0, 0) will not work since the outline will simply be treated as
                // an empty outline.
                outline.setOval(-1, -1, 0, 0);
            }
        });
        view.setClipToOutline(true);

        if (palette != null) {
            view.findViewById(R.id.outgoing_call_animation_circle).setBackgroundColor(
                    palette.mPrimaryColor);
            getWindow().setStatusBarColor(palette.mSecondaryColor);
        }

        view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                final ViewTreeObserver vto = view.getViewTreeObserver();
                if (vto.isAlive()) {
                    vto.removeOnPreDrawListener(this);
                }
                final Animator animator = getRevealAnimator(touchPoint);
                // Since this animator is a RenderNodeAnimator (native animator), add an arbitary
                // start delay to force the onAnimationStart callback to happen later on the UI
                // thread. Otherwise it would happen right away inside animator.start()
                animator.setStartDelay(5);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        InCallPresenter.getInstance().onCircularRevealStarted(
                                CircularRevealActivity.this);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setClipToOutline(false);
                        super.onAnimationEnd(animation);
                    }
                });
                animator.start();
                return false;
            }
        });
    }

    private void clearDisplay() {
        getWindow().getDecorView().setVisibility(View.INVISIBLE);
        finish();
    }

    @Override
    public void onBackPressed() {
        return;
    }

    public static void sendClearDisplayBroadcast(Context context) {
        LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(ACTION_CLEAR_DISPLAY));
    }

    private Animator getRevealAnimator(Point touchPoint) {
        final View view  = getWindow().getDecorView();
        final Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);

        int startX = size.x / 2;
        int startY = size.y / 2;
        if (touchPoint != null) {
            startX = touchPoint.x;
            startY = touchPoint.y;
        }

        final Animator valueAnimator = ViewAnimationUtils.createCircularReveal(view,
                startX, startY, 0, Math.max(size.x, size.y));
        valueAnimator.setDuration(REVEAL_DURATION);
        return valueAnimator;
    }
}