summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/SnackBarManager.java
blob: e1079996bd197a3b32eb408757bb225c8591345f (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
/*
 * Copyright (C) 2015 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.messaging.ui;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewPropertyAnimator;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.android.messaging.Factory;
import com.android.messaging.R;
import com.android.messaging.ui.SnackBar.Placement;
import com.android.messaging.ui.SnackBar.SnackBarListener;
import com.android.messaging.util.AccessibilityUtil;
import com.android.messaging.util.Assert;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.OsUtil;
import com.android.messaging.util.TextUtil;
import com.android.messaging.util.UiUtils;
import com.google.common.base.Joiner;

import java.util.List;

public class SnackBarManager {

    private static SnackBarManager sInstance;

    public static SnackBarManager get() {
        if (sInstance == null) {
            synchronized (SnackBarManager.class) {
                if (sInstance == null) {
                    sInstance = new SnackBarManager();
                }
            }
        }
        return sInstance;
    }

    private final Runnable mDismissRunnable = new Runnable() {
        @Override
        public void run() {
            dismiss();
        }
    };

    private final OnTouchListener mDismissOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            // Dismiss the {@link SnackBar} but don't consume the event.
            dismiss();
            return false;
        }
    };

    private final SnackBarListener mDismissOnUserTapListener = new SnackBarListener() {
        @Override
        public void onActionClick() {
            dismiss();
        }
    };

    private final int mTranslationDurationMs;
    private final Handler mHideHandler;

    private SnackBar mCurrentSnackBar;
    private SnackBar mLatestSnackBar;
    private SnackBar mNextSnackBar;
    private boolean mIsCurrentlyDismissing;
    private PopupWindow mPopupWindow;

    private SnackBarManager() {
        mTranslationDurationMs = Factory.get().getApplicationContext().getResources().getInteger(
                R.integer.snackbar_translation_duration_ms);
        mHideHandler = new Handler();
    }

    public SnackBar getLatestSnackBar() {
        return mLatestSnackBar;
    }

    public SnackBar.Builder newBuilder(final View parentView) {
        return new SnackBar.Builder(this, parentView);
    }

    /**
     * The given snackBar is not guaranteed to be shown. If the previous snackBar is animating away,
     * and another snackBar is requested to show after this one, this snackBar will be skipped.
     */
    public void show(final SnackBar snackBar) {
        Assert.notNull(snackBar);

        if (mCurrentSnackBar != null) {
            LogUtil.d(LogUtil.BUGLE_TAG, "Showing snack bar, but currentSnackBar was not null.");

            // Dismiss the current snack bar. That will cause the next snack bar to be shown on
            // completion.
            mNextSnackBar = snackBar;
            mLatestSnackBar = snackBar;
            dismiss();
            return;
        }

        mCurrentSnackBar = snackBar;
        mLatestSnackBar = snackBar;

        // We want to know when either button was tapped so we can dismiss.
        snackBar.setListener(mDismissOnUserTapListener);

        // Cancel previous dismisses & set dismiss for the delay time.
        mHideHandler.removeCallbacks(mDismissRunnable);
        mHideHandler.postDelayed(mDismissRunnable, snackBar.getDuration());

        snackBar.setEnabled(false);

        // For some reason, the addView function does not respect layoutParams.
        // We need to explicitly set it first here.
        final View rootView = snackBar.getRootView();

        if (LogUtil.isLoggable(LogUtil.BUGLE_TAG, LogUtil.DEBUG)) {
            LogUtil.d(LogUtil.BUGLE_TAG, "Showing snack bar: " + snackBar);
        }
        // Measure the snack bar root view so we know how much to translate by.
        measureSnackBar(snackBar);
        mPopupWindow = new PopupWindow(snackBar.getContext());
        mPopupWindow.setWidth(LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(null);
        mPopupWindow.setContentView(rootView);
        final Placement placement = snackBar.getPlacement();
        if (placement == null) {
            mPopupWindow.showAtLocation(
                    snackBar.getParentView(), Gravity.BOTTOM | Gravity.START,
                    0, getScreenBottomOffset(snackBar));
        } else {
            final View anchorView = placement.getAnchorView();

            // You'd expect PopupWindow.showAsDropDown to ensure the popup moves with the anchor
            // view, which it does for scrolling, but not layout changes, so we have to manually
            // update while the snackbar is showing
            final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mPopupWindow.update(anchorView, 0, getRelativeOffset(snackBar),
                            anchorView.getWidth(), LayoutParams.WRAP_CONTENT);
                }
            };
            anchorView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
            mPopupWindow.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    anchorView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
                }
            });
            mPopupWindow.showAsDropDown(anchorView, 0, getRelativeOffset(snackBar));
        }


        // Animate the toast bar into view.
        placeSnackBarOffScreen(snackBar);
        animateSnackBarOnScreen(snackBar).withEndAction(new Runnable() {
            @Override
            public void run() {
                mCurrentSnackBar.setEnabled(true);
                makeCurrentSnackBarDismissibleOnTouch();
                // Fire an accessibility event as needed
                String snackBarText = snackBar.getMessageText();
                if (!TextUtils.isEmpty(snackBarText) &&
                        TextUtils.getTrimmedLength(snackBarText) > 0) {
                    snackBarText = snackBarText.trim();
                    final String snackBarActionText = snackBar.getActionLabel();
                    if (!TextUtil.isAllWhitespace(snackBarActionText)) {
                        snackBarText = Joiner.on(", ").join(snackBarText, snackBarActionText);
                    }
                    AccessibilityUtil.announceForAccessibilityCompat(snackBar.getSnackBarView(),
                            null /*accessibilityManager*/, snackBarText);
                }
            }
        });

        // Animate any interaction views out of the way.
        animateInteractionsOnShow(snackBar);
    }

    /**
     * Dismisses the current toast that is showing. If there is a toast waiting to be shown, that
     * toast will be shown when the current one has been dismissed.
     */
    public void dismiss() {
        mHideHandler.removeCallbacks(mDismissRunnable);

        if (mCurrentSnackBar == null || mIsCurrentlyDismissing) {
            return;
        }

        final SnackBar snackBar = mCurrentSnackBar;

        LogUtil.d(LogUtil.BUGLE_TAG, "Dismissing snack bar.");
        mIsCurrentlyDismissing = true;

        snackBar.setEnabled(false);

        // Animate the toast bar down.
        final View rootView = snackBar.getRootView();
        animateSnackBarOffScreen(snackBar).withEndAction(new Runnable() {
            @Override
            public void run() {
                rootView.setVisibility(View.GONE);
                try {
                    mPopupWindow.dismiss();
                } catch (IllegalArgumentException e) {
                    // PopupWindow.dismiss() will fire an IllegalArgumentException if the activity
                    // has already ended while we were animating
                }

                mCurrentSnackBar = null;
                mIsCurrentlyDismissing = false;

                // Show the next toast if one is waiting.
                if (mNextSnackBar != null) {
                    final SnackBar localNextSnackBar = mNextSnackBar;
                    mNextSnackBar = null;
                    show(localNextSnackBar);
                }
            }
        });

        // Animate any interaction views back.
        animateInteractionsOnDismiss(snackBar);
    }

    private void makeCurrentSnackBarDismissibleOnTouch() {
        // Set touching on the entire view, the {@link SnackBar} itself, as
        // well as the button's dismiss the toast.
        mCurrentSnackBar.getRootView().setOnTouchListener(mDismissOnTouchListener);
        mCurrentSnackBar.getSnackBarView().setOnTouchListener(mDismissOnTouchListener);
    }

    private void measureSnackBar(final SnackBar snackBar) {
        final View rootView = snackBar.getRootView();
        final Point displaySize = new Point();
        getWindowManager(snackBar.getContext()).getDefaultDisplay().getSize(displaySize);
        final int widthSpec = ViewGroup.getChildMeasureSpec(
                MeasureSpec.makeMeasureSpec(displaySize.x, MeasureSpec.EXACTLY),
                0, LayoutParams.MATCH_PARENT);
        final int heightSpec = ViewGroup.getChildMeasureSpec(
                MeasureSpec.makeMeasureSpec(displaySize.y, MeasureSpec.EXACTLY),
                0, LayoutParams.WRAP_CONTENT);
        rootView.measure(widthSpec, heightSpec);
    }

    private void placeSnackBarOffScreen(final SnackBar snackBar) {
        final View rootView = snackBar.getRootView();
        final View snackBarView = snackBar.getSnackBarView();
        snackBarView.setTranslationY(rootView.getMeasuredHeight());
    }

    private ViewPropertyAnimator animateSnackBarOnScreen(final SnackBar snackBar) {
        final View snackBarView = snackBar.getSnackBarView();
        return normalizeAnimator(snackBarView.animate()).translationX(0).translationY(0);
    }

    private ViewPropertyAnimator animateSnackBarOffScreen(final SnackBar snackBar) {
        final View rootView = snackBar.getRootView();
        final View snackBarView = snackBar.getSnackBarView();
        return normalizeAnimator(snackBarView.animate()).translationY(rootView.getHeight());
    }

    private void animateInteractionsOnShow(final SnackBar snackBar) {
        final List<SnackBarInteraction> interactions = snackBar.getInteractions();
        for (final SnackBarInteraction interaction : interactions) {
            if (interaction != null) {
                final ViewPropertyAnimator animator = interaction.animateOnSnackBarShow(snackBar);
                if (animator != null) {
                    normalizeAnimator(animator);
                }
            }
        }
    }

    private void animateInteractionsOnDismiss(final SnackBar snackBar) {
        final List<SnackBarInteraction> interactions = snackBar.getInteractions();
        for (final SnackBarInteraction interaction : interactions) {
            if (interaction != null) {
                final ViewPropertyAnimator animator =
                        interaction.animateOnSnackBarDismiss(snackBar);
                if (animator != null) {
                    normalizeAnimator(animator);
                }
            }
        }
    }

    private ViewPropertyAnimator normalizeAnimator(final ViewPropertyAnimator animator) {
        return animator
                .setInterpolator(UiUtils.DEFAULT_INTERPOLATOR)
                .setDuration(mTranslationDurationMs);
    }

    private WindowManager getWindowManager(final Context context) {
        return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    /**
     * Get the offset from the bottom of the screen where the snack bar should be placed.
     */
    private int getScreenBottomOffset(final SnackBar snackBar) {
        final WindowManager windowManager = getWindowManager(snackBar.getContext());
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        if (OsUtil.isAtLeastL()) {
            windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
        } else {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        }
        final int screenHeight = displayMetrics.heightPixels;

        if (OsUtil.isAtLeastL()) {
            // In L, the navigation bar is included in the space for the popup window, so we have to
            // offset by the size of the navigation bar
            final Rect displayRect = new Rect();
            snackBar.getParentView().getRootView().getWindowVisibleDisplayFrame(displayRect);
            return screenHeight - displayRect.bottom;
        }

        return 0;
    }

    private int getRelativeOffset(final SnackBar snackBar) {
        final Placement placement = snackBar.getPlacement();
        Assert.notNull(placement);
        final View anchorView = placement.getAnchorView();
        if (placement.getAnchorAbove()) {
            return -snackBar.getRootView().getMeasuredHeight() - anchorView.getHeight();
        } else {
            // Use the default dropdown positioning
            return 0;
        }
    }
}