Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit e79003a

Browse files
committed
Added option to show default dialog after update check
1 parent 2292ea5 commit e79003a

4 files changed

Lines changed: 92 additions & 3 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ android {
2626

2727
dependencies {
2828
testCompile 'junit:junit:4.12'
29+
compile 'com.android.support:appcompat-v7:25.1.1'
2930
compile 'io.reactivex:rxjava:1.2.5'
31+
compile 'io.reactivex:rxandroid:1.2.1'
32+
3033
}
3134

3235
apply from: rootProject.file('gradle/gradle-mvn-push.gradle')

library/src/main/java/com/ackee/versioupdatehandler/VersionStatusResolver.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
package com.ackee.versioupdatehandler;
22

3+
import android.app.Dialog;
4+
import android.content.DialogInterface;
5+
import android.content.Intent;
6+
import android.net.Uri;
7+
import android.os.Bundle;
8+
import android.support.annotation.NonNull;
9+
import android.support.v4.app.DialogFragment;
10+
import android.support.v4.app.FragmentManager;
11+
import android.support.v7.app.AlertDialog;
312
import android.util.Log;
413

514
import com.ackee.versioupdatehandler.model.VersionStatus;
615
import com.ackee.versioupdatehandler.model.VersionsConfiguration;
716

817
import rx.Single;
18+
import rx.android.schedulers.AndroidSchedulers;
19+
import rx.functions.Action1;
920
import rx.functions.Func1;
21+
import rx.schedulers.Schedulers;
1022

1123

1224
/**
@@ -46,4 +58,80 @@ private VersionStatus resolveStatus(int version, VersionsConfiguration configura
4658
" current version: " + configuration.minimalVersion() + " Status resolved: " + status);
4759
return status;
4860
}
61+
62+
public void checkVersionStatusAndOpenDefault(final int version, final FragmentManager fragmentManager) {
63+
checkVersionStatus(version)
64+
.subscribeOn(Schedulers.newThread())
65+
.observeOn(AndroidSchedulers.mainThread())
66+
.subscribe(new Action1<VersionStatus>() {
67+
@Override
68+
public void call(VersionStatus versionStatus) {
69+
if (versionStatus != VersionStatus.UP_TO_DATE) {
70+
showDialog(versionStatus == VersionStatus.UPDATE_REQUIRED, fragmentManager);
71+
}
72+
}
73+
}, new Action1<Throwable>() {
74+
@Override
75+
public void call(Throwable throwable) {
76+
throwable.printStackTrace();
77+
}
78+
});
79+
}
80+
81+
/**
82+
* Show dialog with update info
83+
*
84+
* @param forceUpdate indicator if update is mandatory
85+
*/
86+
private void showDialog(boolean forceUpdate, FragmentManager fragmentManager) {
87+
UpdateDialog.newInstance(forceUpdate).show(fragmentManager, UpdateDialog.class.getName());
88+
}
89+
90+
public static class UpdateDialog extends DialogFragment {
91+
92+
private static final String FORCE_UPDATE_KEY = "force_update";
93+
94+
public static UpdateDialog newInstance(boolean forceUpdate) {
95+
Bundle args = new Bundle();
96+
args.putBoolean(FORCE_UPDATE_KEY, forceUpdate);
97+
UpdateDialog updateDialog = new UpdateDialog();
98+
updateDialog.setArguments(args);
99+
return updateDialog;
100+
}
101+
102+
@NonNull
103+
@Override
104+
public Dialog onCreateDialog(Bundle savedInstanceState) {
105+
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
106+
builder.setTitle("Update app");
107+
builder.setMessage("Your application is outdated. Please update to the newest version");
108+
final boolean isForceUpdate = getArguments().getBoolean(FORCE_UPDATE_KEY, false);
109+
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
110+
@Override
111+
public void onClick(DialogInterface dialog, int which) {
112+
if (isForceUpdate) {
113+
getActivity().finish();
114+
}
115+
final String appPackageName = getActivity().getPackageName(); // getPackageName() from Context or Activity object
116+
try {
117+
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
118+
} catch (android.content.ActivityNotFoundException anfe) {
119+
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
120+
}
121+
}
122+
});
123+
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
124+
@Override
125+
public void onClick(DialogInterface dialog, int which) {
126+
if (isForceUpdate) {
127+
getActivity().moveTaskToBack(true);
128+
}
129+
}
130+
});
131+
return builder.create();
132+
}
133+
}
134+
49135
}
136+
137+

rest-fetcher/src/main/java/com/ackee/versionupdatehandler/RestVersionFetcher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import rx.Single;
1818
import rx.functions.Func1;
1919

20-
import static com.ackee.versionupdatehandler.RestVersionFetcher.ApiDescription.api;
21-
2220
/**
2321
* Class that fetches version configuration from rest api
2422
* <p>

0 commit comments

Comments
 (0)