CheckUpdate.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package net.indivia.hacklabbo.statusreader;
  2. import org.json.JSONObject;
  3. import android.annotation.SuppressLint;
  4. import android.annotation.TargetApi;
  5. import android.app.Notification;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.app.Service;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.res.Resources;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.os.IBinder;
  15. import android.provider.Settings;
  16. import android.util.Log;
  17. @SuppressLint("NewApi")
  18. public class CheckUpdate extends Service {
  19. /*private final static String app_site = "http://isopen.hacklabbo.indivia.net/";*/
  20. private final static String LOG_TAG = "CheckUpdate";
  21. private final static String app_apk = "net.indivia.hacklabbo.statusreader";
  22. private final static String app_site = "http://ginex.indivia.net/";
  23. private final static String app_www_current_apk = "latest.apk";
  24. private final static String app_www_current_version = "update.json";
  25. //DEBUG:
  26. private final static String app_www_current_version_fail = "update-fail.json";
  27. private final static String app_www_current_version_true = "update-true.json";
  28. public final static int NOTIFY_APP_UPDATE = 2;
  29. private BgThread bgThread;
  30. private NotificationManager notificationManager;
  31. private Notification notification;
  32. private PendingIntent pIntent;
  33. private int notificationNumber;
  34. private Resources res;
  35. @Override
  36. public void onCreate() {
  37. super.onCreate();
  38. res = getResources();
  39. bgThread = new BgThread();
  40. bgThread.start();
  41. Intent intent = new Intent(this, Downloader.class);
  42. String app_uri = app_site + app_www_current_apk;
  43. intent.putExtra("checkUpdate", NOTIFY_APP_UPDATE);
  44. intent.setData(Uri.parse(app_uri));
  45. intent.putExtra("urlpath", app_uri);
  46. pIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
  47. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
  48. notifyBuilder();
  49. else
  50. getNotify();
  51. notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  52. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  53. Log.i(LOG_TAG, "Service Created");
  54. }
  55. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  56. public void notifyBuilder(){
  57. notification = new Notification.Builder(this)
  58. .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
  59. .setContentTitle(getString(R.string.app_update_notify_title))
  60. .setContentText(getString(R.string.app_update_new_version))
  61. .setLights(0xFFFF0000, 500, 500)
  62. .setSmallIcon(R.drawable.software)
  63. .setContentIntent(pIntent)
  64. .build();
  65. }
  66. @SuppressWarnings("deprecation")
  67. public void getNotify(){
  68. notification = new Notification(
  69. R.drawable.software,
  70. getString(R.string.app_update_notify_title),
  71. System.currentTimeMillis());
  72. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  73. }
  74. @SuppressWarnings("deprecation")
  75. public void sendNotification(int newVersion) {
  76. notification.number=++notificationNumber;
  77. notification.setLatestEventInfo (this,
  78. res.getString(R.string.app_update_notify_title),
  79. res.getString(R.string.app_update_new_version),
  80. pIntent);
  81. notificationManager.notify(NOTIFY_APP_UPDATE, notification);
  82. }
  83. private final class BgThread extends Thread {
  84. public void run() {
  85. try {
  86. //String json = SNetwork.GET(app_site + app_www_current_version);
  87. String json = SNetwork.GET(app_site + app_www_current_version_true);
  88. JSONObject jObject = new JSONObject(json);
  89. int newVersion = jObject.getInt("version");
  90. int curVersion = getPackageManager().getPackageInfo(app_apk, 0).versionCode;
  91. //Log.d(LOG_TAG,"cur:"+curVersion+" new:"+newVersion);
  92. if (newVersion > curVersion){
  93. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
  94. notificationManager.notify(0, notification);
  95. else
  96. sendNotification(newVersion);
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. stopSelf();
  102. }
  103. }
  104. @Override
  105. public int onStartCommand(Intent intent, int flags, int startId) {
  106. Log.i(LOG_TAG, "Service Started");
  107. return super.onStartCommand(intent, flags, startId);
  108. }
  109. @Override
  110. public void onDestroy() {
  111. super.onDestroy();
  112. Log.i(LOG_TAG, "Service Destroyed");
  113. }
  114. @Override
  115. public IBinder onBind(Intent intent) {
  116. return null;
  117. }
  118. }