15
15
*******************************************************************************/
16
16
package tr .com .turkcellteknoloji .turkcellupdater ;
17
17
18
+ import android .content .Context ;
19
+
18
20
import org .json .JSONObject ;
19
21
20
22
import java .net .MalformedURLException ;
21
23
import java .net .URL ;
24
+ import java .util .Date ;
22
25
import java .util .Iterator ;
23
26
import java .util .List ;
24
27
import java .util .Vector ;
@@ -27,17 +30,20 @@ class UpdateEntry extends FilteredEntry {
27
30
28
31
final List <UpdateDescription > updateDescriptions ;
29
32
33
+ final int id ;
34
+
30
35
final int targetVersionCode ;
31
36
final String targetPackageName ;
32
37
33
38
final URL targetPackageUrl ;
34
39
final URL targetWebsiteUrl ;
35
40
final boolean targetGooglePlay ;
36
-
41
+ final int displayPeriodInHours ;
42
+ final int maxDisplayCount ;
37
43
final boolean forceUpdate ;
38
44
final boolean forceExit ;
39
45
40
- UpdateEntry (List <Filter > filters , List <UpdateDescription > updateDescriptions , int targetVersionCode , String targetPackageName , URL targetPackageUrl , URL targetWebsiteUrl , boolean targetGooglePlay , boolean forceUpdate , boolean forceExit ) throws UpdaterException {
46
+ UpdateEntry (List <Filter > filters , int id , List <UpdateDescription > updateDescriptions , int displayPeriodInHours , int maxDisplayCount , int targetVersionCode , String targetPackageName , URL targetPackageUrl , URL targetWebsiteUrl , boolean targetGooglePlay , boolean forceUpdate , boolean forceExit ) throws UpdaterException {
41
47
super (filters );
42
48
this .updateDescriptions = updateDescriptions ;
43
49
this .targetVersionCode = targetVersionCode ;
@@ -47,6 +53,9 @@ class UpdateEntry extends FilteredEntry {
47
53
this .targetGooglePlay = targetGooglePlay ;
48
54
this .forceUpdate = forceUpdate ;
49
55
this .forceExit = forceExit ;
56
+ this .displayPeriodInHours = displayPeriodInHours ;
57
+ this .maxDisplayCount = maxDisplayCount ;
58
+ this .id = id == 0 ? generateId () : id ;
50
59
validate ();
51
60
}
52
61
@@ -60,9 +69,23 @@ class UpdateEntry extends FilteredEntry {
60
69
this .targetWebsiteUrl = getUrl (jsonObject , "targetWebsiteUrl" );
61
70
this .targetPackageName = Utilities .removeWhiteSpaces (jsonObject .optString ("targetPackageName" ));
62
71
this .targetGooglePlay = jsonObject .optBoolean ("targetGooglePlay" );
72
+ this .displayPeriodInHours = jsonObject .optInt ("displayPeriodInHours" , 0 );
73
+ this .maxDisplayCount = jsonObject .optInt ("maxDisplayCount" , Integer .MAX_VALUE );
74
+ int i = jsonObject .optInt ("id" , 0 );
75
+ this .id = i == 0 ? generateId () : i ;
63
76
validate ();
64
77
}
65
78
79
+ private int generateId () {
80
+ final int prime = 31 ;
81
+ int result = 1 ;
82
+ result = prime * result + (targetGooglePlay ? 1231 : 1237 );
83
+ result = prime * result + ((targetPackageName == null ) ? 0 : targetPackageName .hashCode ());
84
+ result = prime * result + ((targetWebsiteUrl == null ) ? 0 : targetWebsiteUrl .hashCode ());
85
+ result = prime * result + ((updateDescriptions == null ) ? 0 : updateDescriptions .hashCode ());
86
+ return result ;
87
+ }
88
+
66
89
private static URL getUrl (JSONObject jsonObject , String key ) throws UpdaterException {
67
90
String spec = Utilities .removeWhiteSpaces (jsonObject .optString (key ));
68
91
if ("" .equals (spec )) {
@@ -98,7 +121,8 @@ private static List<UpdateDescription> createUpdateDescritions(JSONObject jsonOb
98
121
return result ;
99
122
}
100
123
101
- Update getUpdate (Properties properties ) throws UpdaterException {
124
+ Update getUpdate (Properties properties , UpdateDisplayRecords records ) throws UpdaterException {
125
+ Date now = new Date ();
102
126
String languageCode = null ;
103
127
if (properties != null ) {
104
128
final String s = properties .getValue (Properties .KEY_DEVICE_LANGUAGE );
@@ -113,7 +137,25 @@ Update getUpdate(Properties properties) throws UpdaterException {
113
137
if (Utilities .isNullOrEmpty (packageName )) {
114
138
throw new UpdaterException ("'packageName' property should not be null or empty." );
115
139
}
140
+ if (maxDisplayCount < Integer .MAX_VALUE ) {
141
+ final int count = records .getUpdateDisplayCount (id );
142
+ if (count >= maxDisplayCount ) {
143
+ return null ;
144
+ }
145
+ }
146
+ // check if it is displayed earlier than specified period
147
+ if (displayPeriodInHours > 0 ) {
148
+ final Date updateLastDisplayDate = records .getUpdateLastDisplayDate (id );
149
+ // check if message displayed before
150
+ if (updateLastDisplayDate != null ) {
151
+ final Date date = Utilities .addHours (now , -displayPeriodInHours );
152
+ if (updateLastDisplayDate .after (date )) {
153
+ return null ;
154
+ }
155
+ }
156
+ }
116
157
final UpdateDescription updateDescription = LocalizedStringMap .select (updateDescriptions , languageCode );
158
+ records .onUpdateDisplayed (id , now );
117
159
return new Update (updateDescription , targetPackageUrl , targetWebsiteUrl , targetGooglePlay , targetVersionCode , packageName , forceUpdate , forceExit );
118
160
}
119
161
@@ -129,12 +171,31 @@ private void validate() throws UpdaterException {
129
171
}
130
172
}
131
173
132
- boolean shouldDisplay (Properties properties ) {
174
+ boolean shouldDisplay (Properties properties , UpdateDisplayRecords records , Context context ) {
133
175
if (isMatches (properties )) {
134
176
final Integer currentVersionCode = Utilities .tryParseInteger (properties .getValue (Properties .KEY_APP_VERSION_CODE ));
135
177
if (currentVersionCode != null && targetVersionCode != currentVersionCode .intValue ()) {
136
178
return true ;
137
179
}
180
+ // check if it is displayed more than specified count
181
+ if (maxDisplayCount < Integer .MAX_VALUE ) {
182
+ final int count = records .getUpdateDisplayCount (id );
183
+ if (count >= maxDisplayCount ) {
184
+ return false ;
185
+ }
186
+ }
187
+ // check if it is displayed earlier than specified period
188
+ if (displayPeriodInHours > 0 ) {
189
+ final Date updateLastDisplayDate = records .getUpdateLastDisplayDate (id );
190
+ // check if update displayed before
191
+ if (updateLastDisplayDate != null ) {
192
+ Date now = new Date ();
193
+ final Date date = Utilities .addHours (now , -displayPeriodInHours );
194
+ if (updateLastDisplayDate .after (date )) {
195
+ return false ;
196
+ }
197
+ }
198
+ }
138
199
final String currentPackageName = properties .getValue (Properties .KEY_APP_PACKAGE_NAME );
139
200
if (!Utilities .isNullOrEmpty (currentPackageName ) && !Utilities .isNullOrEmpty (targetPackageName )) {
140
201
return !currentPackageName .equals (targetPackageName );
0 commit comments