Skip to content

Instantly share code, notes, and snippets.

@m7mdra
Last active June 19, 2018 11:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m7mdra/976bd482c73ea5fc1dec064b372b2c34 to your computer and use it in GitHub Desktop.
Save m7mdra/976bd482c73ea5fc1dec064b372b2c34 to your computer and use it in GitHub Desktop.
checks google play for newer version using Jsoup and rxandroid
package m7mdra.io.services.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import m7mdra.io.services.BuildConfig;
import m7mdra.io.services.R;
public class VersionChecker {
private static String CURRENT_VERSION= BuildConfig.VERSION_NAME;
private Activity activity;
private final String appPackageName;
public VersionChecker(Activity activity){
this.activity=activity;
appPackageName = activity.getPackageName();
}
public void check(){
Observable.fromCallable(new Callable<Elements>() {
@Override
public Elements call() throws Exception {
Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id="+appPackageName).get();
return doc.getElementsByAttributeValue("itemprop","softwareVersion");
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Elements>() {
@Override
public void accept(Elements s) throws Exception {
PLogger.p(s.text());
if (!s.text().equals(CURRENT_VERSION))
if (!activity.isFinishing()) //to avoid crashing when activiy is not visible
showUpdate().setMessage("Show message here").create().show();
}
});
}
private AlertDialog.Builder showUpdate(){
return new AlertDialog.Builder(activity)
.setTitle("title here")
.setCancelable(false)
.setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
}catch (android.content.ActivityNotFoundException ignored) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
ignored.printStackTrace();
}
}
});
}
}
package m7mdra.io.services.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import m7mdra.io.services.BuildConfig;
import m7mdra.io.services.R;
public class VersionChecker implements Runnable {
private final String CURRENT_VERSION= BuildConfig.VERSION_NAME;
private Activity activity;
private final String appPackageName;
public VersionChecker(Activity activity){
this.activity=activity;
appPackageName = activity.getPackageName();
}
private AlertDialog.Builder showUpdate(){
return new AlertDialog.Builder(activity)
.setTitle("title here")
.setCancelable(false)
.setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
}catch (android.content.ActivityNotFoundException ignored) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
ignored.printStackTrace();
}
}
});
}
@Override
public void run() {
try {
Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id="+appPackageName).get();
Elements s = doc.getElementsByAttributeValue("itemprop","softwareVersion");
PLogger.p(s.text());
if (!s.text().equals(CURRENT_VERSION))
if (!activity.isFinishing())
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
showUpdate().setMessage("message here").create().show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment