-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathMaterialShowCaseAdapter.java
More file actions
187 lines (155 loc) · 6.24 KB
/
MaterialShowCaseAdapter.java
File metadata and controls
187 lines (155 loc) · 6.24 KB
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
package uk.co.deanwild.materialshowcaseview.adapter;
import android.app.Activity;
import android.os.Handler;
import android.support.annotation.ArrayRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import uk.co.deanwild.materialshowcaseview.MaterialShowcaseSequence;
import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView;
import uk.co.deanwild.materialshowcaseview.ShowcaseConfig;
/**
* Created by TurboCoder (Yamil García Hernández) on 7/7/16.
*/
public abstract class MaterialShowCaseAdapter implements MaterialShowcaseSequence.OnSequenceItemDismissedListener {
// Constants
protected String SHOW_CASE_ID;
public final static int SHOW_CASE_DELAY = 5;
public static final ArrayList<MaterialShowcaseSequence> POOL = new ArrayList<>();
// Variables
protected Activity activity;
private MaterialShowcaseSequence sequence;
protected ShowcaseConfig config;
private ArrayList<MaterialShowcaseView> queue = new ArrayList<>();
protected int currentQueueItemId = 0;
public MaterialShowCaseAdapter(Activity activity, int delay, String id) {
this.activity = activity;
this.config = new ShowcaseConfig();
this.SHOW_CASE_ID = id;
config.setDelay(SHOW_CASE_DELAY);
sequence = new MaterialShowcaseSequence(activity, id);
sequence.setConfig(config);
}
public abstract void setup();
public void start() {
setup();
MaterialShowCaseAdapter.POOL.add(sequence);
sequence.setOnItemDismissedListener(this);
if (POOL.size() <= 1)
sequence.start();
}
public static void initialize(Activity activity, Class clazz) {
try {
Constructor<?> constructor = clazz.getConstructor(Activity.class);
Object obj = constructor.newInstance(new Object[]{activity});
if (obj instanceof MaterialShowCaseAdapter)
((MaterialShowCaseAdapter) obj).start();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void initializeWithDelay(final Activity activity, final Class clazz, long delay) {
try {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Constructor<?> constructor = clazz.getConstructor(Activity.class);
Object obj = constructor.newInstance(new Object[]{activity});
if (obj instanceof MaterialShowCaseAdapter)
((MaterialShowCaseAdapter) obj).start();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}, delay);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void initialize(Activity activity, @ArrayRes int config, String id) {
try {
(new SimpleMaterialShowCaseAdapter(activity, activity.getResources().obtainTypedArray(config), id)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void initializeWithDelay(final Activity activity, @ArrayRes final int config, final String id, long delay) {
try {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
(new SimpleMaterialShowCaseAdapter(activity, activity.getResources().obtainTypedArray(config), id)).start();
}
}, delay);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addToQueue(MaterialShowcaseView v) {
queue.add(v);
sequence.addSequenceItem(v);
}
public void addToQueue(@IdRes int i, String title, String content, String button, MaterialShowCaseViewShape shape) {
MaterialShowcaseView.Builder msvb = new MaterialShowcaseView.Builder(activity)
.setTarget(activity.findViewById(i))
.setTitleText(title)
.setContentText(content)
.setDismissText(button);
switch (shape) {
case CIRCLE:
msvb.withCircleShape();
break;
case RECTANGLE:
msvb.withRectangleShape();
break;
}
MaterialShowcaseView msv = msvb.build();
queue.add(msv);
sequence.addSequenceItem(msv);
}
public void addToQueue(@IdRes int i, @StringRes int title, @StringRes int content, @StringRes int button, MaterialShowCaseViewShape shape) {
MaterialShowcaseView.Builder msvb = new MaterialShowcaseView.Builder(activity)
.setTarget(activity.findViewById(i))
.setTitleText(title)
.setContentText(content)
.setDismissText(button);
switch (shape) {
case CIRCLE:
msvb.withCircleShape();
break;
case RECTANGLE:
msvb.withRectangleShape();
break;
}
MaterialShowcaseView msv = msvb.build();
queue.add(msv);
sequence.addSequenceItem(msv);
}
@Override
public void onDismiss(MaterialShowcaseView materialShowcaseView, int i) {
if (currentQueueItemId == (queue.size() - 1)) {
MaterialShowCaseAdapter.POOL.remove(0);
if (POOL.size() > 0)
MaterialShowCaseAdapter.POOL.get(0).start();
}
currentQueueItemId++;
}
public enum MaterialShowCaseViewShape {
CIRCLE, RECTANGLE
}
}