This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathAppRate.java
More file actions
248 lines (196 loc) · 6.9 KB
/
AppRate.java
File metadata and controls
248 lines (196 loc) · 6.9 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
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
package hotchemi.android.rate;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static hotchemi.android.rate.DialogManager.create;
import static hotchemi.android.rate.PreferenceHelper.getInstallDate;
import static hotchemi.android.rate.PreferenceHelper.getIsAgreeShowDialog;
import static hotchemi.android.rate.PreferenceHelper.getLaunchTimes;
import static hotchemi.android.rate.PreferenceHelper.getRemindInterval;
import static hotchemi.android.rate.PreferenceHelper.isFirstLaunch;
import static hotchemi.android.rate.PreferenceHelper.setInstallDate;
import static hotchemi.android.rate.PreferenceHelper.getCustomEventCount;
import static hotchemi.android.rate.PreferenceHelper.setCustomEventCount;
public final class AppRate {
private static AppRate singleton;
private final Context context;
private final DialogOptions options = new DialogOptions();
private int installDate = 10;
private int launchTimes = 10;
private int remindInterval = 1;
private final HashMap<String, Long> customEventCounts = new HashMap<>();
private boolean isDebug = false;
private AppRate(Context context) {
this.context = context.getApplicationContext();
}
public static AppRate with(Context context) {
if (singleton == null) {
synchronized (AppRate.class) {
if (singleton == null) {
singleton = new AppRate(context);
}
}
}
return singleton;
}
public static boolean showRateDialogIfMeetsConditions(Activity activity) {
boolean isMeetsConditions = singleton.isDebug || singleton.shouldShowRateDialog();
if (isMeetsConditions) {
singleton.showRateDialog(activity);
}
return isMeetsConditions;
}
private static boolean isOverDate(long targetDate, int threshold) {
return new Date().getTime() - targetDate >= threshold * 24 * 60 * 60 * 1000;
}
public AppRate setLaunchTimes(int launchTimes) {
this.launchTimes = launchTimes;
return this;
}
public AppRate setInstallDays(int installDate) {
this.installDate = installDate;
return this;
}
public AppRate setRemindInterval(int remindInterval) {
this.remindInterval = remindInterval;
return this;
}
public AppRate setMinimumEventCount(String eventName, long minimumCount) {
this.customEventCounts.put(eventName, minimumCount);
return this;
}
public AppRate setShowLaterButton(boolean isShowNeutralButton) {
options.setShowNeutralButton(isShowNeutralButton);
return this;
}
public AppRate setShowNeverButton(boolean isShowNeverButton) {
options.setShowNegativeButton(isShowNeverButton);
return this;
}
public AppRate setShowTitle(boolean isShowTitle) {
options.setShowTitle(isShowTitle);
return this;
}
public AppRate clearAgreeShowDialog() {
PreferenceHelper.setAgreeShowDialog(context, true);
return this;
}
public AppRate clearSettingsParam() {
PreferenceHelper.setAgreeShowDialog(context, true);
PreferenceHelper.clearSharedPreferences(context);
return this;
}
public AppRate setAgreeShowDialog(boolean clear) {
PreferenceHelper.setAgreeShowDialog(context, clear);
return this;
}
public AppRate setView(View view) {
options.setView(view);
return this;
}
public AppRate setOnClickButtonListener(OnClickButtonListener listener) {
options.setListener(listener);
return this;
}
public AppRate setTitle(int resourceId) {
options.setTitleResId(resourceId);
return this;
}
public AppRate setTitle(String title) {
options.setTitleText(title);
return this;
}
public AppRate setMessage(int resourceId) {
options.setMessageResId(resourceId);
return this;
}
public AppRate setMessage(String message) {
options.setMessageText(message);
return this;
}
public AppRate setTextRateNow(int resourceId) {
options.setTextPositiveResId(resourceId);
return this;
}
public AppRate setTextRateNow(String positiveText) {
options.setPositiveText(positiveText);
return this;
}
public AppRate setTextLater(int resourceId) {
options.setTextNeutralResId(resourceId);
return this;
}
public AppRate setTextLater(String neutralText) {
options.setNeutralText(neutralText);
return this;
}
public AppRate setTextNever(int resourceId) {
options.setTextNegativeResId(resourceId);
return this;
}
public AppRate setTextNever(String negativeText) {
options.setNegativeText(negativeText);
return this;
}
public AppRate setCancelable(boolean cancelable) {
options.setCancelable(cancelable);
return this;
}
public AppRate setStoreType(StoreType appstore) {
options.setStoreType(appstore);
return this;
}
public AppRate incrementEventCount(String eventName) {
return setEventCountValue(eventName, getCustomEventCount(context,eventName) + 1);
}
public AppRate setEventCountValue(String eventName, long countValue) {
setCustomEventCount(context, eventName, countValue);
return this;
}
public void monitor() {
if (isFirstLaunch(context)) {
setInstallDate(context);
}
PreferenceHelper.setLaunchTimes(context, getLaunchTimes(context) + 1);
}
public void showRateDialog(Activity activity) {
if (!activity.isFinishing()) {
create(activity, options).show();
}
}
public boolean shouldShowRateDialog() {
return getIsAgreeShowDialog(context) &&
isOverLaunchTimes() &&
isOverInstallDate() &&
isOverRemindDate() &&
isOverCustomEventRequirements();
}
private boolean isOverLaunchTimes() {
return getLaunchTimes(context) >= launchTimes;
}
private boolean isOverInstallDate() {
return isOverDate(getInstallDate(context), installDate);
}
private boolean isOverRemindDate() {
return isOverDate(getRemindInterval(context), remindInterval);
}
private boolean isOverCustomEventRequirements() {
for(Map.Entry<String, Long> eventRequirement : customEventCounts.entrySet()) {
Long currentCount = getCustomEventCount(context, eventRequirement.getKey());
if(currentCount < eventRequirement.getValue()) {
return false;
}
}
return true;
}
public boolean isDebug() {
return isDebug;
}
public AppRate setDebug(boolean isDebug) {
this.isDebug = isDebug;
return this;
}
}