1 /*
2  * Copyright 2018 Google LLC All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.skar.examples.helloskar.helpers;
18 
19 import android.app.Activity;
20 import android.support.design.widget.BaseTransientBottomBar;
21 import android.support.design.widget.Snackbar;
22 import android.view.View;
23 
24 /**
25  * Helper to manage the sample snackbar. Hides the Android boilerplate code, and exposes simpler
26  * methods.
27  */
28 public final class SnackbarHelper {
29     private static final int BACKGROUND_COLOR = 0xbf323232;
30     private Snackbar messageSnackbar;
31 
32     private enum DismissBehavior {HIDE, SHOW, FINISH}
33 
34     ;
35 
isShowing()36     public boolean isShowing() {
37         return messageSnackbar != null;
38     }
39 
40     /**
41      * Shows a snackbar with a given message.
42      */
showMessage(Activity activity, String message)43     public void showMessage(Activity activity, String message) {
44         show(activity, message, DismissBehavior.HIDE);
45     }
46 
47     /**
48      * Shows a snackbar with a given message, and a dismiss button.
49      */
showMessageWithDismiss(Activity activity, String message)50     public void showMessageWithDismiss(Activity activity, String message) {
51         show(activity, message, DismissBehavior.SHOW);
52     }
53 
54     /**
55      * Shows a snackbar with a given error message. When dismissed, will finish the activity. Useful
56      * for notifying errors, where no further interaction with the activity is possible.
57      */
showError(Activity activity, String errorMessage)58     public void showError(Activity activity, String errorMessage) {
59         show(activity, errorMessage, DismissBehavior.FINISH);
60     }
61 
62     /**
63      * Hides the currently showing snackbar, if there is one. Safe to call from any thread. Safe to
64      * call even if snackbar is not shown.
65      */
hide(Activity activity)66     public void hide(Activity activity) {
67         activity.runOnUiThread(
68                 new Runnable() {
69                     @Override
70                     public void run() {
71                         if (messageSnackbar != null) {
72                             messageSnackbar.dismiss();
73                         }
74                         messageSnackbar = null;
75                     }
76                 });
77     }
78 
show( final Activity activity, final String message, final DismissBehavior dismissBehavior)79     private void show(
80             final Activity activity, final String message, final DismissBehavior dismissBehavior) {
81         activity.runOnUiThread(
82                 new Runnable() {
83                     @Override
84                     public void run() {
85                         messageSnackbar =
86                                 Snackbar.make(
87                                         activity.findViewById(android.R.id.content),
88                                         message,
89                                         Snackbar.LENGTH_INDEFINITE);
90                         messageSnackbar.getView().setBackgroundColor(BACKGROUND_COLOR);
91                         if (dismissBehavior != DismissBehavior.HIDE) {
92                             messageSnackbar.setAction(
93                                     "Dismiss",
94                                     new View.OnClickListener() {
95                                         @Override
96                                         public void onClick(View v) {
97                                             messageSnackbar.dismiss();
98                                         }
99                                     });
100                             if (dismissBehavior == DismissBehavior.FINISH) {
101                                 messageSnackbar.addCallback(
102                                         new BaseTransientBottomBar.BaseCallback<Snackbar>() {
103                                             @Override
104                                             public void onDismissed(Snackbar transientBottomBar, int event) {
105                                                 super.onDismissed(transientBottomBar, event);
106                                                 activity.finish();
107                                             }
108                                         });
109                             }
110                         }
111                         messageSnackbar.show();
112                     }
113                 });
114     }
115 }
116