UpdateTask.java 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package me.yoqi.app.wxredpacket.utils;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.pm.PackageInfo;
  5. import android.net.Uri;
  6. import android.os.AsyncTask;
  7. import android.widget.Toast;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.StatusLine;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.json.JSONObject;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import me.yoqi.app.wxredpacket.R;
  17. public class UpdateTask extends AsyncTask<String, String, String> {
  18. public static int count = 0;
  19. private Context context;
  20. private boolean isUpdateOnRelease;
  21. public static final String updateUrl = "https://api.github.com/repos/geeeeeeeeek/WeChatLuckyMoney/releases/latest";
  22. public UpdateTask(Context context, boolean needUpdate) {
  23. this.context = context;
  24. this.isUpdateOnRelease = needUpdate;
  25. if (this.isUpdateOnRelease) Toast.makeText(context, context.getString(R.string.checking_new_version), Toast.LENGTH_SHORT).show();
  26. }
  27. @Override
  28. protected String doInBackground(String... uri) {
  29. HttpClient httpclient = new DefaultHttpClient();
  30. HttpResponse response;
  31. String responseString = null;
  32. try {
  33. response = httpclient.execute(new HttpGet(uri[0]));
  34. StatusLine statusLine = response.getStatusLine();
  35. if (statusLine.getStatusCode() == 200) {
  36. ByteArrayOutputStream out = new ByteArrayOutputStream();
  37. response.getEntity().writeTo(out);
  38. responseString = out.toString();
  39. out.close();
  40. } else {
  41. // Close the connection.
  42. response.getEntity().getContent().close();
  43. throw new IOException(statusLine.getReasonPhrase());
  44. }
  45. } catch (Exception e) {
  46. return null;
  47. }
  48. return responseString;
  49. }
  50. @Override
  51. protected void onPostExecute(String result) {
  52. super.onPostExecute(result);
  53. try {
  54. count += 1;
  55. JSONObject release = new JSONObject(result);
  56. // Get current version
  57. PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
  58. String version = pInfo.versionName;
  59. String latestVersion = release.getString("tag_name");
  60. boolean isPreRelease = release.getBoolean("prerelease");
  61. if (!isPreRelease && version.compareToIgnoreCase(latestVersion) >= 0) {
  62. // Your version is ahead of or same as the latest.
  63. if (this.isUpdateOnRelease)
  64. Toast.makeText(context, R.string.update_already_latest, Toast.LENGTH_SHORT).show();
  65. } else {
  66. if (!isUpdateOnRelease) {
  67. Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg3), Toast.LENGTH_LONG).show();
  68. return;
  69. }
  70. // Need update.
  71. String downloadUrl = release.getJSONArray("assets").getJSONObject(0).getString("browser_download_url");
  72. // Give up on the fucking DownloadManager. The downloaded apk got renamed and unable to install. Fuck.
  73. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));
  74. browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  75. context.startActivity(browserIntent);
  76. Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg2), Toast.LENGTH_LONG).show();
  77. }
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. if (this.isUpdateOnRelease) Toast.makeText(context, R.string.update_error, Toast.LENGTH_LONG).show();
  81. }
  82. }
  83. public void update() {
  84. super.execute(updateUrl);
  85. }
  86. }