|
package com.geneticcoder.Versionator; |
|
|
|
// Check here for updates: https://gist.github.com/Pamblam/53cb2b1c90dea09d4872084956d17b5c |
|
|
|
import android.content.Context; |
|
import android.content.DialogInterface; |
|
import android.content.Intent; |
|
import android.content.pm.PackageManager; |
|
import android.net.Uri; |
|
import android.os.AsyncTask; |
|
import android.os.Bundle; |
|
import android.util.Log; |
|
import android.app.AlertDialog; |
|
|
|
import com.squareup.okhttp.OkHttpClient; |
|
import com.squareup.okhttp.Request; |
|
import com.squareup.okhttp.Response; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
public class Versionator { |
|
|
|
private Context context; |
|
|
|
public static String packageName = null; |
|
|
|
public Versionator(Context c){ |
|
context = c; |
|
packageName = context.getPackageName(); |
|
} |
|
|
|
public Versionator setPackageName(String packageName){ |
|
this.packageName = packageName; |
|
return this; |
|
} |
|
|
|
public void checkUpdate(){ |
|
new AsyncTask<String, Void, Bundle>() { |
|
@Override |
|
protected Bundle doInBackground(String... params) { |
|
String html = "<div class=\"content\" itemprop=\"softwareVersion\"> 0 </div>"; |
|
try { |
|
|
|
OkHttpClient client = new OkHttpClient(); |
|
Request request = new Request.Builder() |
|
.url("https://play.google.com/store/apps/details?id="+packageName+"&hl=en") |
|
.build(); |
|
Response response = client.newCall(request).execute(); |
|
html = response.body().string(); |
|
}catch (IOException e){e.printStackTrace();} |
|
html = html.substring(html.indexOf("Current Version</div><div><span class=\"")+46); |
|
html = html.substring(0,html.indexOf("<")); |
|
float vn = Float.valueOf(html); |
|
float cvn = 0; |
|
try { |
|
String vname = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; |
|
cvn = Float.valueOf(vname); |
|
}catch (PackageManager.NameNotFoundException e){e.printStackTrace();} |
|
Bundle b = new Bundle(); |
|
b.putFloat("LatestVersion",vn); |
|
b.putFloat("CurrentVersion",cvn); |
|
b.putBoolean("UpdateAvailable", vn>cvn); |
|
return b; |
|
} |
|
@Override |
|
protected void onPostExecute(Bundle result) { |
|
if(result.getBoolean("UpdateAvailable")){ |
|
updateAvailable(result); |
|
}else{ |
|
done(); |
|
} |
|
} |
|
}.execute(); |
|
} |
|
|
|
// override this... |
|
public void done(){ |
|
Log.i("Versionator", "No updates available"); |
|
} |
|
|
|
public void updateAvailable(Bundle updateInfo){ |
|
AlertDialog alertDialog = new AlertDialog.Builder(context).create(); |
|
alertDialog.setCancelable(false); |
|
alertDialog.setCanceledOnTouchOutside(false); |
|
alertDialog.setTitle("Update Available!"); |
|
alertDialog.setMessage("Version "+updateInfo.getFloat("LatestVersion")+" has been released. "+ |
|
"You are currently using version "+updateInfo.getFloat("CurrentVersion")+". "+ |
|
"Would you like to update now?"); |
|
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Update", |
|
new DialogInterface.OnClickListener() { |
|
public void onClick(DialogInterface dialog, int which) { |
|
try { |
|
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))); |
|
} catch (android.content.ActivityNotFoundException anfe) { |
|
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName))); |
|
} |
|
dialog.dismiss(); |
|
} |
|
}); |
|
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Not now", |
|
new DialogInterface.OnClickListener() { |
|
public void onClick(DialogInterface dialog, int which) { |
|
done(); |
|
dialog.dismiss(); |
|
} |
|
}); |
|
alertDialog.show(); |
|
Log.i("Versionator", "Updates available"); |
|
} |
|
|
|
} |