-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShadowSnackbar.java
135 lines (104 loc) · 4.05 KB
/
ShadowSnackbar.java
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
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.snackbar.ContentViewCallback;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.SnackbarContentLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* Created on 10/31/17.
*/
@SuppressWarnings({"UnusedDeclaration", "Unchecked"})
@Implements(Snackbar.class)
public class ShadowSnackbar {
static List<ShadowSnackbar> shadowSnackbars = new ArrayList<>();
@RealObject
Snackbar snackbar;
String text;
private int duration;
private int gravity;
private int xOffset;
private int yOffset;
private View view;
@Implementation
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration) {
Snackbar snackbar = null;
try {
Constructor<Snackbar> constructor = Snackbar.class.getDeclaredConstructor(ViewGroup.class, View.class, ContentViewCallback.class);
if (null == constructor)
throw new IllegalArgumentException("Seems like the constructor was not found!");
if (Modifier.isPrivate(constructor.getModifiers())) {
constructor.setAccessible(true);
}
ViewGroup parent = findSuitableParent(view);
final SnackbarContentLayout content =
(SnackbarContentLayout)
LayoutInflater.from(parent.getContext()).inflate(android.support.design.R.layout.design_layout_snackbar_include, parent, false);
snackbar = constructor.newInstance(parent, content, content);
snackbar.setText(text);
snackbar.setDuration(duration);
} catch (Exception e) {
e.printStackTrace();
}
shadowOf(snackbar).text = text.toString();
shadowSnackbars.add(shadowOf(snackbar));
return snackbar;
}
private static ViewGroup findSuitableParent(View view) {
ViewGroup fallback = null;
do {
if (view instanceof CoordinatorLayout) {
return (ViewGroup) view;
} else if (view instanceof FrameLayout) {
if (view.getId() == android.R.id.content) {
return (ViewGroup) view;
} else {
fallback = (ViewGroup) view;
}
}
if (view != null) {
final ViewParent parent = view.getParent();
if (parent == null) {
fallback = new FrameLayout(view.getContext());
}
view = parent instanceof View ? (View) parent : null;
}
} while (view != null);
return fallback;
}
@Implementation
public static Snackbar make(@NonNull View view, @StringRes int resId, int duration) {
return make(view, view.getResources().getText(resId), duration);
}
static ShadowSnackbar shadowOf(Snackbar bar) {
return (ShadowSnackbar) Shadow.extract(bar);
}
public static void reset() {
shadowSnackbars.clear();
}
public static int shownSnackbarCount() {
return shadowSnackbars.isEmpty() ? 0 : shadowSnackbars.size();
}
public static String getTextOfLatestSnackbar() {
if (!shadowSnackbars.isEmpty())
return shadowSnackbars.get(shadowSnackbars.size() - 1).text;
return null;
}
public static Snackbar getLatestSnackbar() {
if (!shadowSnackbars.isEmpty())
return shadowSnackbars.get(shadowSnackbars.size() - 1).snackbar;
return null;
}
}