Skip to content

Commit e4ff0ce

Browse files
committed
- add force update interface
1 parent 0729145 commit e4ff0ce

File tree

8 files changed

+119
-54
lines changed

8 files changed

+119
-54
lines changed

app/src/main/java/com/android/sebiya/update/MainActivity.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void show(final Activity activity, final AppUpdateInfo appUpdateInfo, Pac
5858
// make custom display such as notification, dialog, snackbar, toast, etc...
5959
String message = "TikTok app has " + (appUpdateInfo.hasAvailableUpdates() ? "update" : "no update");
6060
if (appUpdateInfo.hasAvailableUpdates()) {
61-
message += "\nlatest version - " + appUpdateInfo.getLatestVersionName();
61+
message += "\nlatest version - " + appUpdateInfo.getServerVersionName();
6262
}
6363
// make custom display such as notification, dialog, snackbar, toast, etc...
6464
new AlertDialog.Builder(activity)
@@ -161,7 +161,7 @@ public void onClick(final View v) {
161161
public void show(final Activity activity, final AppUpdateInfo appUpdateInfo, PackageInstaller installer) {
162162
String message = "TikTok app has " + (appUpdateInfo.hasAvailableUpdates() ? "update" : "no update");
163163
if (appUpdateInfo.hasAvailableUpdates()) {
164-
message += "\nlatest version - " + appUpdateInfo.getLatestVersionName();
164+
message += "\nlatest version - " + appUpdateInfo.getServerVersionName();
165165
}
166166
// make custom display such as notification, dialog, snackbar, toast, etc...
167167
new AlertDialog.Builder(activity)
@@ -268,11 +268,10 @@ private static class GithubStringToAppUpdateInfoConverter implements Converter {
268268

269269
@Override
270270
public AppUpdateInfo convert(final String data) {
271-
AppUpdateInfo appUpdateInfo = new AppUpdateInfo(targetVersionCode, targetVersionName);
272271
VersionSample versionSample = new Gson().fromJson(data, VersionSample.class);
273-
appUpdateInfo.setLatestVersionCode(versionSample.latestVersion);
274-
appUpdateInfo.setLatestVersionName(versionSample.latestVersionName);
275-
return appUpdateInfo;
272+
return new AppUpdateInfo.Builder(null)
273+
.withCurrentVersion(targetVersionCode, targetVersionName)
274+
.withServerVersion(versionSample.latestVersion, versionSample.latestVersionName).build();
276275
}
277276
}
278277
}

library/src/main/java/com/android/sebiya/update/AppUpdateChecker.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public void start(final Activity activity) {
7575
loadAppUpdateInfo()
7676
.subscribeOn(Schedulers.io())
7777
.observeOn(AndroidSchedulers.mainThread())
78-
.map(updateVersionInfo())
7978
.map(showUi(activity))
8079
.subscribe(new Consumer<Boolean>() {
8180
@Override
@@ -111,18 +110,6 @@ public void subscribe(final SingleEmitter<AppUpdateInfo> emitter) {
111110
});
112111
}
113112

114-
private Function<AppUpdateInfo, AppUpdateInfo> updateVersionInfo() {
115-
return new Function<AppUpdateInfo, AppUpdateInfo>() {
116-
@Override
117-
public AppUpdateInfo apply(final AppUpdateInfo appUpdateInfo) {
118-
appUpdateInfo.setHasAvailableUpdates(mVersionChecker.hasAvailableUpdates(appUpdateInfo));
119-
if (mEnableLog) {
120-
Log.d(LOG_TAG, "updateVersionInfo. data - " + appUpdateInfo);
121-
}
122-
return appUpdateInfo;
123-
}
124-
};
125-
}
126113

127114
private Function<AppUpdateInfo, Boolean> showUi(final Activity activity) {
128115
return new Function<AppUpdateInfo, Boolean>() {
Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,112 @@
11
package com.android.sebiya.update;
22

3-
public final class AppUpdateInfo {
3+
import android.content.Context;
4+
import android.content.pm.PackageInfo;
5+
import android.content.pm.PackageManager.NameNotFoundException;
6+
import android.support.annotation.Nullable;
7+
import android.util.Log;
8+
import com.android.sebiya.update.AppVersionChecker.DefaultImpl;
49

5-
private long latestVersionCode;
10+
public final class AppUpdateInfo {
611

7-
private String latestVersionName;
12+
private static final String LOG_TAG = "AppUpdateInfo";
813

9-
private long targetVersionCode;
10-
private String targetVersionName;
14+
private Builder mBuilder;
1115

12-
private boolean hasAvailableUpdates;
16+
private AppUpdateInfo(Builder builder) {
17+
mBuilder = builder;
18+
}
1319

14-
public AppUpdateInfo(int targetVersionCode, String targetVersionName) {
15-
this.targetVersionCode = targetVersionCode;
16-
this.targetVersionName = targetVersionName;
20+
public long getCurrentVersionCode() {
21+
return mBuilder.currentVersionCode;
1722
}
1823

19-
public long getTargetVersionCode() {
20-
return targetVersionCode;
24+
public String getCurrentVersionName() {
25+
return mBuilder.currentVersionName;
2126
}
2227

23-
public String getTargetVersionName() {
24-
return targetVersionName;
28+
public long getServerVersionCode() {
29+
return mBuilder.serverVersionCode;
2530
}
2631

27-
public void setLatestVersionCode(final long latestVersionCode) {
28-
this.latestVersionCode = latestVersionCode;
32+
public String getServerVersionName() {
33+
return mBuilder.serverVersionName;
2934
}
3035

31-
public long getLatestVersionCode() {
32-
return latestVersionCode;
36+
public long getForceUpdateVersionCode() {
37+
return mBuilder.forceUpdateVersionCode;
3338
}
3439

35-
public String getLatestVersionName() {
36-
return latestVersionName;
40+
public String getForceUpdateVersionName() {
41+
return mBuilder.forceUpdateVersionName;
3742
}
3843

39-
public void setLatestVersionName(final String latestVersionName) {
40-
this.latestVersionName = latestVersionName;
44+
45+
public boolean hasAvailableUpdates() {
46+
return mBuilder.appVersionChecker.hasAvailableUpdates(this);
4147
}
4248

43-
public void setHasAvailableUpdates(final boolean hasAvailableUpdates) {
44-
this.hasAvailableUpdates = hasAvailableUpdates;
49+
public boolean hasForceUpdates() {
50+
return mBuilder.appVersionChecker.hasForceUpdates(this);
4551
}
4652

47-
public boolean hasAvailableUpdates() {
48-
return hasAvailableUpdates;
53+
public static class Builder {
54+
55+
private long serverVersionCode;
56+
private String serverVersionName;
57+
private long currentVersionCode;
58+
private String currentVersionName;
59+
private String forceUpdateVersionName;
60+
private long forceUpdateVersionCode;
61+
private AppVersionChecker appVersionChecker;
62+
private final Context context;
63+
64+
65+
public Builder(@Nullable Context context) {
66+
this.context = context;
67+
}
68+
69+
public Builder withVersionChecker(AppVersionChecker checker) {
70+
appVersionChecker = checker;
71+
return this;
72+
}
73+
74+
public Builder withCurrentVersion(long code, String name) {
75+
currentVersionCode = code;
76+
currentVersionName = name;
77+
return this;
78+
}
79+
80+
public Builder withServerVersion(long code, String name) {
81+
serverVersionCode = code;
82+
serverVersionName = name;
83+
return this;
84+
}
85+
86+
public Builder withForceUpdate(long code, String name) {
87+
forceUpdateVersionCode = code;
88+
forceUpdateVersionName = name;
89+
return this;
90+
}
91+
92+
public AppUpdateInfo build() {
93+
if (appVersionChecker == null) {
94+
appVersionChecker = new DefaultImpl();
95+
}
96+
if (currentVersionCode == 0 || currentVersionName == null) {
97+
if (context == null) {
98+
throw new IllegalArgumentException("context or current version code should be set");
99+
}
100+
try {
101+
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
102+
currentVersionName = packageInfo.versionName;
103+
currentVersionCode = packageInfo.versionCode;
104+
Log.i(LOG_TAG, "build. name - " + currentVersionCode + ", code - " + currentVersionCode);
105+
} catch (NameNotFoundException e) {
106+
e.printStackTrace();
107+
}
108+
}
109+
return new AppUpdateInfo(this);
110+
}
49111
}
50112
}

library/src/main/java/com/android/sebiya/update/AppVersionChecker.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface AppVersionChecker {
2121

2222
boolean hasAvailableUpdates(AppUpdateInfo appUpdateInfo);
2323

24+
boolean hasForceUpdates(AppUpdateInfo appUpdateInfo);
25+
2426
AppVersionChecker DEFAULT = new DefaultImpl();
2527

2628
/**
@@ -30,16 +32,30 @@ class DefaultImpl implements AppVersionChecker {
3032

3133
@Override
3234
public boolean hasAvailableUpdates(final AppUpdateInfo appUpdateInfo) {
33-
if (appUpdateInfo.getLatestVersionCode() > 0) {
34-
return appUpdateInfo.getTargetVersionCode() < appUpdateInfo.getLatestVersionCode();
35+
if (appUpdateInfo.getServerVersionCode() > 0) {
36+
return appUpdateInfo.getCurrentVersionCode() < appUpdateInfo.getServerVersionCode();
37+
}
38+
39+
if (appUpdateInfo.getCurrentVersionName() == null || appUpdateInfo.getServerVersionName() == null) {
40+
return false;
41+
}
42+
43+
Version version = new Version(appUpdateInfo.getServerVersionName());
44+
return version.isHigherThan(appUpdateInfo.getCurrentVersionName());
45+
}
46+
47+
@Override
48+
public boolean hasForceUpdates(final AppUpdateInfo appUpdateInfo) {
49+
if (appUpdateInfo.getForceUpdateVersionCode() > 0) {
50+
return appUpdateInfo.getCurrentVersionCode() < appUpdateInfo.getForceUpdateVersionCode();
3551
}
3652

37-
if (appUpdateInfo.getTargetVersionName() == null || appUpdateInfo.getLatestVersionName() == null) {
53+
if (appUpdateInfo.getCurrentVersionName() == null || appUpdateInfo.getForceUpdateVersionName() == null) {
3854
return false;
3955
}
4056

41-
Version version = new Version(appUpdateInfo.getLatestVersionName());
42-
return version.isHigherThan(appUpdateInfo.getTargetVersionName());
57+
Version version = new Version(appUpdateInfo.getForceUpdateVersionName());
58+
return version.isHigherThan(appUpdateInfo.getCurrentVersionName());
4359
}
4460
}
4561
}

library/src/main/java/com/android/sebiya/update/data/GooglePlayDataSource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public AppUpdateInfo load() {
6363
.ownText();
6464
Log.d(LOG_TAG, "load. package - " + packageName + ", version name from web - " + versionName);
6565
if (versionName != null) {
66-
info = new AppUpdateInfo(-1, targetVersionName);
67-
info.setLatestVersionName(versionName);
66+
info = new AppUpdateInfo.Builder(null)
67+
.withCurrentVersion(0, targetVersionName)
68+
.withServerVersion(0, versionName).build();
6869
}
6970
} catch (IOException e) {
7071
e.printStackTrace();

library/src/main/java/com/android/sebiya/update/ui/SimpleDialogDisplay.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected String getTitle(AppUpdateInfo appUpdateInfo) {
2424
}
2525

2626
protected String getMessage(AppUpdateInfo appUpdateInfo) {
27-
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getLatestVersionName()
27+
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getServerVersionName()
2828
+ " is available!" : "No updates available";
2929
}
3030

library/src/main/java/com/android/sebiya/update/ui/SimpleSnackbarDisplay.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void onClick(final View v) {
4949
}
5050

5151
protected String getMessage(AppUpdateInfo appUpdateInfo) {
52-
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getLatestVersionName()
52+
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getServerVersionName()
5353
+ " is available!" : "No updates available";
5454
}
5555

library/src/main/java/com/android/sebiya/update/ui/SimpleToastDisplay.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void show(final Activity activity, final AppUpdateInfo appUpdateInfo, Pac
1313
}
1414

1515
protected String getMessage(AppUpdateInfo appUpdateInfo) {
16-
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getLatestVersionName()
16+
return appUpdateInfo.hasAvailableUpdates() ? "Update " + appUpdateInfo.getServerVersionName()
1717
+ " is available!" : "No updates available";
1818
}
1919

0 commit comments

Comments
 (0)