Skip to content

Commit 51379f6

Browse files
Merge pull request #9 from menekseibrahim/master
maxDisplayCount and displayPeriodInHours attributes have been activated for updates.
2 parents 5b48f8c + ec3d76d commit 51379f6

File tree

5 files changed

+693
-524
lines changed

5 files changed

+693
-524
lines changed

TurkcellUpdaterSampleApp/turkcellUpdater/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="tr.com.turkcellteknoloji.turkcellupdater"
3-
android:versionCode="3"
4-
android:versionName="1.2.2" >
3+
android:versionCode="4"
4+
android:versionName="1.2.3" >
55

66
<uses-sdk
77
android:minSdkVersion="8"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2013 Turkcell
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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+
package tr.com.turkcellteknoloji.turkcellupdater;
17+
18+
import android.annotation.SuppressLint;
19+
import android.content.Context;
20+
import android.content.SharedPreferences;
21+
import android.content.SharedPreferences.Editor;
22+
23+
import java.util.Date;
24+
import java.util.HashSet;
25+
import java.util.Map;
26+
import java.util.Set;
27+
28+
/**
29+
* Keeps information about displayed updates like last display date and display count.
30+
* <br>
31+
* Data kept by this class is backed by an {@link SharedPreferences} object named {@value #SHARED_PREFERENCES_NAME}
32+
* @author Ugur Ozmen
33+
*
34+
*/
35+
class UpdateDisplayRecords {
36+
37+
static final String SHARED_PREFERENCES_NAME = "turkcell-updater-update-display-records";
38+
39+
private final SharedPreferences sharedPreferences;
40+
41+
/**
42+
* Creates a new instance
43+
* @param context
44+
*/
45+
UpdateDisplayRecords(Context context) {
46+
sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
47+
}
48+
49+
/**
50+
* Returns display count of update with given <code>id</code>
51+
* @param id ID of queried update
52+
* @return total display count.
53+
*/
54+
int getUpdateDisplayCount(int id) {
55+
return sharedPreferences.getInt(id + "-display-count", 0);
56+
}
57+
58+
/**
59+
* Returns last display time of update with given <code>id</code>
60+
* @param id ID of queried update
61+
* @return last display date or <code>null</code> if update is not displayed yet.
62+
*/
63+
Date getUpdateLastDisplayDate(int id) {
64+
final long millis = sharedPreferences.getLong(id + "-last-display-date", 0);
65+
if (millis == 0) {
66+
return null;
67+
} else {
68+
return new Date(millis);
69+
}
70+
}
71+
72+
/**
73+
* Increases display count of update with given <code>id</code> by one and stores current time as last display date for the update.
74+
* @param id ID of queried update
75+
*/
76+
@SuppressLint("NewApi")
77+
void onUpdateDisplayed(int id) {
78+
onUpdateDisplayed(id, new Date());
79+
}
80+
81+
/**
82+
* Test friendly version of {@link #onUpdateDisplayed(int)}.
83+
* <br>
84+
* <em><strong>Note:</strong> This method is should only be used for testing purposes.</em>
85+
* @param id
86+
* @param now
87+
*/
88+
@SuppressLint("NewApi")
89+
@Deprecated
90+
void onUpdateDisplayed(int id, Date now) {
91+
final int updateDisplayCount = getUpdateDisplayCount(id);
92+
final Editor edit = sharedPreferences.edit();
93+
edit.putInt(id + "-display-count", updateDisplayCount + 1);
94+
edit.putLong(id + "-last-display-date", now.getTime());
95+
if (android.os.Build.VERSION.SDK_INT > 8) {
96+
edit.apply();
97+
} else {
98+
edit.commit();
99+
}
100+
}
101+
102+
/**
103+
* Deletes record for given id.
104+
* <br>
105+
* <em><strong>Note:</strong> This method is should only be used for testing purposes.</em>
106+
* @param id
107+
*/
108+
@Deprecated
109+
void deleteUpdateRecords(int id) {
110+
final Editor edit = sharedPreferences.edit();
111+
edit.remove(id + "-display-count");
112+
edit.remove(id + "-last-display-date");
113+
edit.commit();
114+
}
115+
116+
/**
117+
* Deletes all records.
118+
* <br>
119+
* <em><strong>Note:</strong> This method is should only be used for testing purposes.</em>
120+
*/
121+
@Deprecated
122+
void deleteAllRecords() {
123+
final Map<String, ?> all = sharedPreferences.getAll();
124+
Set<String> keys = new HashSet<String>(all.keySet());
125+
final Editor edit = sharedPreferences.edit();
126+
for (String key : keys) {
127+
edit.remove(key);
128+
}
129+
edit.commit();
130+
}
131+
}

TurkcellUpdaterSampleApp/turkcellUpdater/src/main/java/tr/com/turkcellteknoloji/turkcellupdater/UpdateEntry.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*******************************************************************************/
1616
package tr.com.turkcellteknoloji.turkcellupdater;
1717

18+
import android.content.Context;
19+
1820
import org.json.JSONObject;
1921

2022
import java.net.MalformedURLException;
2123
import java.net.URL;
24+
import java.util.Date;
2225
import java.util.Iterator;
2326
import java.util.List;
2427
import java.util.Vector;
@@ -27,17 +30,20 @@ class UpdateEntry extends FilteredEntry {
2730

2831
final List<UpdateDescription> updateDescriptions;
2932

33+
final int id;
34+
3035
final int targetVersionCode;
3136
final String targetPackageName;
3237

3338
final URL targetPackageUrl;
3439
final URL targetWebsiteUrl;
3540
final boolean targetGooglePlay;
36-
41+
final int displayPeriodInHours;
42+
final int maxDisplayCount;
3743
final boolean forceUpdate;
3844
final boolean forceExit;
3945

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 {
4147
super(filters);
4248
this.updateDescriptions = updateDescriptions;
4349
this.targetVersionCode = targetVersionCode;
@@ -47,6 +53,9 @@ class UpdateEntry extends FilteredEntry {
4753
this.targetGooglePlay = targetGooglePlay;
4854
this.forceUpdate = forceUpdate;
4955
this.forceExit = forceExit;
56+
this.displayPeriodInHours = displayPeriodInHours;
57+
this.maxDisplayCount = maxDisplayCount;
58+
this.id = id == 0 ? generateId() : id;
5059
validate();
5160
}
5261

@@ -60,9 +69,23 @@ class UpdateEntry extends FilteredEntry {
6069
this.targetWebsiteUrl = getUrl(jsonObject, "targetWebsiteUrl");
6170
this.targetPackageName = Utilities.removeWhiteSpaces(jsonObject.optString("targetPackageName"));
6271
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;
6376
validate();
6477
}
6578

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+
6689
private static URL getUrl(JSONObject jsonObject, String key) throws UpdaterException {
6790
String spec = Utilities.removeWhiteSpaces(jsonObject.optString(key));
6891
if ("".equals(spec)) {
@@ -98,7 +121,8 @@ private static List<UpdateDescription> createUpdateDescritions(JSONObject jsonOb
98121
return result;
99122
}
100123

101-
Update getUpdate(Properties properties) throws UpdaterException {
124+
Update getUpdate(Properties properties, UpdateDisplayRecords records) throws UpdaterException {
125+
Date now = new Date();
102126
String languageCode = null;
103127
if (properties != null) {
104128
final String s = properties.getValue(Properties.KEY_DEVICE_LANGUAGE);
@@ -113,7 +137,25 @@ Update getUpdate(Properties properties) throws UpdaterException {
113137
if (Utilities.isNullOrEmpty(packageName)) {
114138
throw new UpdaterException("'packageName' property should not be null or empty.");
115139
}
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+
}
116157
final UpdateDescription updateDescription = LocalizedStringMap.select(updateDescriptions, languageCode);
158+
records.onUpdateDisplayed(id, now);
117159
return new Update(updateDescription, targetPackageUrl, targetWebsiteUrl, targetGooglePlay, targetVersionCode, packageName, forceUpdate, forceExit);
118160
}
119161

@@ -129,12 +171,31 @@ private void validate() throws UpdaterException {
129171
}
130172
}
131173

132-
boolean shouldDisplay(Properties properties) {
174+
boolean shouldDisplay(Properties properties, UpdateDisplayRecords records, Context context) {
133175
if (isMatches(properties)) {
134176
final Integer currentVersionCode = Utilities.tryParseInteger(properties.getValue(Properties.KEY_APP_VERSION_CODE));
135177
if (currentVersionCode != null && targetVersionCode != currentVersionCode.intValue()) {
136178
return true;
137179
}
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+
}
138199
final String currentPackageName = properties.getValue(Properties.KEY_APP_PACKAGE_NAME);
139200
if (!Utilities.isNullOrEmpty(currentPackageName) && !Utilities.isNullOrEmpty(targetPackageName)) {
140201
return !currentPackageName.equals(targetPackageName);

0 commit comments

Comments
 (0)