StatusUpdate.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package net.indivia.hacklabbo.statusreader;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Locale;
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7. import android.annotation.SuppressLint;
  8. import android.annotation.TargetApi;
  9. import android.app.Notification;
  10. import android.app.NotificationManager;
  11. import android.app.PendingIntent;
  12. import android.app.Service;
  13. import android.content.Context;
  14. import android.content.Intent;
  15. import android.content.res.Resources;
  16. import android.net.ConnectivityManager;
  17. import android.net.Uri;
  18. import android.os.Build;
  19. import android.os.IBinder;
  20. import android.provider.Settings;
  21. import android.util.Log;
  22. @SuppressLint("NewApi")
  23. public class StatusUpdate extends Service {
  24. // private final static String app_site =
  25. // "http://isopen.hacklabbo.indivia.net/";
  26. private final static String app_site = "http://ginex.indivia.net/";
  27. private final static String LOG_TAG = "StatusUpdate";
  28. // public final static String DEF_SITE =
  29. // "http://isopen.hacklabbo.indivia.net/hacklabbo/state.json";
  30. public final static String DEF_SITE = "http://ginex.indivia.net/state.json";
  31. private final static String app_www_current_apk = "latest.apk";
  32. public final static int NOTIFY_APP_UPDATE = 2;
  33. private BgThread bgThread;
  34. private NotificationManager notificationManager;
  35. private Notification notification;
  36. private PendingIntent pIntent;
  37. private int notificationNumber;
  38. private Resources res;
  39. private static String serverName;
  40. private static Boolean oldStatus, isCicleService;
  41. private static int refreshTime;
  42. private ConnectivityManager cm;
  43. @Override
  44. public void onCreate() {
  45. super.onCreate();
  46. res = getResources();
  47. cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  48. bgThread = new BgThread();
  49. bgThread.start();
  50. String app_uri = app_site + app_www_current_apk;
  51. Intent i = new Intent(this, MainActivity.class);
  52. i.putExtra("checkUpdate", NOTIFY_APP_UPDATE);
  53. i.setData(Uri.parse(app_uri));
  54. i.putExtra("urlpath", app_uri);
  55. pIntent = PendingIntent.getActivity(this, 0, i,
  56. PendingIntent.FLAG_UPDATE_CURRENT);
  57. notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  58. Log.i(LOG_TAG, "Service Created");
  59. }
  60. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  61. public void notifyBuilder(String status) {
  62. notification = new Notification.Builder(this)
  63. .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
  64. // .setContentTitle(getString(R.string.app_update_notify_title))
  65. .setContentTitle("StatusReader: cambio di stato")
  66. .setContentText("Stato cambiato a: " + status)
  67. .setLights(0xFFFF0000, 500, 500)
  68. // TODO: cambiare l'icona
  69. .setSmallIcon(R.drawable.software).setContentIntent(pIntent)
  70. .setAutoCancel(true).build();
  71. }
  72. // TODO: cambiare titolo icone
  73. @SuppressWarnings("deprecation")
  74. public void getNotify() {
  75. notification = new Notification(R.drawable.software,
  76. getString(R.string.app_update_notify_title),
  77. System.currentTimeMillis());
  78. notification.flags |= Notification.FLAG_AUTO_CANCEL;
  79. }
  80. @SuppressWarnings("deprecation")
  81. public void sendNotification(int newVersion) {
  82. notification.number = ++notificationNumber;
  83. notification.setLatestEventInfo(this,
  84. res.getString(R.string.app_update_notify_title),
  85. res.getString(R.string.app_update_new_version), pIntent);
  86. notificationManager.notify(NOTIFY_APP_UPDATE, notification);
  87. }
  88. private final class BgThread extends Thread {
  89. public void run() {
  90. do {
  91. if (SNetwork.isConnected(cm)) {
  92. Log.i(LOG_TAG, "Service Cicle");
  93. String json = SNetwork.GET(serverName);
  94. try {
  95. JSONObject jObject = new JSONObject(json);
  96. boolean newStatus = jObject.getBoolean("open");
  97. String statusChange = jObject.getString("date");
  98. Calendar c = Calendar.getInstance();
  99. SimpleDateFormat dateFormat = new SimpleDateFormat(
  100. "yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ITALY);
  101. String last_refresh = dateFormat.format(c.getTime());
  102. if (newStatus != oldStatus) {
  103. String status = res.getString(R.string.close);
  104. if (newStatus)
  105. status = res.getString(R.string.open);
  106. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  107. notifyBuilder(status);
  108. notificationManager.notify(0, notification);
  109. } else {
  110. getNotify();
  111. sendNotification(1);
  112. }
  113. }
  114. SUtility.saveState(getApplicationContext(), newStatus,
  115. last_refresh, statusChange);
  116. if (!isCicleService) {
  117. stopSelf();
  118. Log.i(LOG_TAG, "Service stop");
  119. // setDisplay(newStatus, last_refresh, statusChange);
  120. }
  121. try {
  122. Thread.sleep(refreshTime);
  123. } catch (InterruptedException ie) {
  124. Log.d(LOG_TAG,
  125. "Qualcosa è andato storto con il timer.\n"
  126. + ie.toString());
  127. }
  128. } catch (JSONException e) {
  129. e.printStackTrace();
  130. }
  131. } else {
  132. Log.d(LOG_TAG, "Non c'è connessione");
  133. }
  134. } while (isCicleService);
  135. }
  136. }
  137. @Override
  138. public int onStartCommand(Intent intent, int flags, int startId) {
  139. isCicleService = intent.getBooleanExtra("isCicleService", false);
  140. if (isCicleService)
  141. refreshTime = intent.getIntExtra("refreshTime", 0);
  142. serverName = intent.getStringExtra("serverName");
  143. oldStatus = intent.getBooleanExtra("oldStatus", false);
  144. Log.i(LOG_TAG, "Service Started");
  145. return super.onStartCommand(intent, flags, startId);
  146. }
  147. @Override
  148. public void onDestroy() {
  149. super.onDestroy();
  150. Log.i(LOG_TAG, "Service Destroyed");
  151. }
  152. @Override
  153. public IBinder onBind(Intent intent) {
  154. return null;
  155. }
  156. }