|
@@ -17,24 +17,27 @@ import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.res.Resources;
|
|
|
import android.net.ConnectivityManager;
|
|
|
-import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
|
import android.provider.Settings;
|
|
|
+import android.support.v4.content.LocalBroadcastManager;
|
|
|
import android.util.Log;
|
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
public class StatusUpdate extends Service {
|
|
|
- // private final static String app_site =
|
|
|
- // "http://isopen.hacklabbo.indivia.net/";
|
|
|
- private final static String app_site = "http://ginex.indivia.net/";
|
|
|
+ public static boolean active = false;
|
|
|
+
|
|
|
private final static String LOG_TAG = "StatusUpdate";
|
|
|
public final static String DEF_SITE =
|
|
|
"http://isopen.hacklabbo.indivia.net/hacklabbo/state.json";
|
|
|
+
|
|
|
+ // private final static String app_site =
|
|
|
+ // "http://isopen.hacklabbo.indivia.net/";
|
|
|
+ //private final static String app_site = "http://ginex.indivia.net/";
|
|
|
//public final static String DEF_SITE = "http://ginex.indivia.net/state.json";
|
|
|
- private final static String app_www_current_apk = "latest.apk";
|
|
|
+ //private final static String app_www_current_apk = "latest.apk";
|
|
|
|
|
|
- public final static int NOTIFY_APP_UPDATE = 2;
|
|
|
+ public final static int NOTIFY_STATUS_UPDATE = 3;
|
|
|
|
|
|
private BgThread bgThread;
|
|
|
private NotificationManager notificationManager;
|
|
@@ -43,9 +46,17 @@ public class StatusUpdate extends Service {
|
|
|
private int notificationNumber;
|
|
|
private Resources res;
|
|
|
private static String serverName;
|
|
|
- private static Boolean oldStatus, isCicleService;
|
|
|
+ private static Boolean oldStatus, isCicleService;
|
|
|
private static int refreshTime;
|
|
|
private ConnectivityManager cm;
|
|
|
+ private final static int MSG_NO_CONN = 0;
|
|
|
+ private final static int MSG_DONE = 1;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
@@ -55,11 +66,12 @@ public class StatusUpdate extends Service {
|
|
|
bgThread = new BgThread();
|
|
|
bgThread.start();
|
|
|
|
|
|
- String app_uri = app_site + app_www_current_apk;
|
|
|
Intent i = new Intent(this, MainActivity.class);
|
|
|
- i.putExtra("checkUpdate", NOTIFY_APP_UPDATE);
|
|
|
- i.setData(Uri.parse(app_uri));
|
|
|
- i.putExtra("urlpath", app_uri);
|
|
|
+ i.putExtra("statusUpdate", NOTIFY_STATUS_UPDATE);
|
|
|
+ //TODO: sta roba non dovrebbe servire
|
|
|
+ //String app_uri = app_site + app_www_current_apk;
|
|
|
+ //i.setData(Uri.parse(app_uri));
|
|
|
+ //i.putExtra("urlpath", app_uri);
|
|
|
|
|
|
pIntent = PendingIntent.getActivity(this, 0, i,
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
@@ -69,6 +81,29 @@ public class StatusUpdate extends Service {
|
|
|
Log.i(LOG_TAG, "Service Created");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ //Send an Intent with an action named "custom-event-name".
|
|
|
+ //The Intent sent should be received by the ReceiverActivity.
|
|
|
+ private void sendMessage(int msg) {
|
|
|
+ Log.i(LOG_TAG, "Send msg to UI");
|
|
|
+ Intent intent = new Intent("filter-updated");
|
|
|
+
|
|
|
+ String msg_str="";
|
|
|
+ switch(msg){
|
|
|
+ case MSG_NO_CONN:
|
|
|
+ msg_str="Manca connessione";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ //msg_str="done";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ intent.putExtra("msg", msg_str);
|
|
|
+ intent.putExtra("isCicleService", isCicleService);
|
|
|
+
|
|
|
+ LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
|
|
+ }
|
|
|
+
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
|
|
public void notifyBuilder(String status) {
|
|
|
notification = new Notification.Builder(this)
|
|
@@ -99,10 +134,10 @@ public class StatusUpdate extends Service {
|
|
|
res.getString(R.string.app_update_notify_title),
|
|
|
res.getString(R.string.app_update_new_version), pIntent);
|
|
|
|
|
|
- notificationManager.notify(NOTIFY_APP_UPDATE, notification);
|
|
|
+ notificationManager.notify(NOTIFY_STATUS_UPDATE, notification);
|
|
|
}
|
|
|
|
|
|
- private final class BgThread extends Thread {
|
|
|
+ public final class BgThread extends Thread {
|
|
|
public void run() {
|
|
|
do {
|
|
|
if (SNetwork.isConnected(cm)) {
|
|
@@ -111,37 +146,44 @@ public class StatusUpdate extends Service {
|
|
|
try {
|
|
|
JSONObject jObject = new JSONObject(json);
|
|
|
boolean newStatus = jObject.getBoolean("open");
|
|
|
- String statusChange = jObject.getString("date");
|
|
|
+ String newChange = jObject.getString("date");
|
|
|
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(
|
|
|
"yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ITALY);
|
|
|
String last_refresh = dateFormat.format(c.getTime());
|
|
|
-
|
|
|
+
|
|
|
if (newStatus != oldStatus) {
|
|
|
String status = res.getString(R.string.close);
|
|
|
if (newStatus)
|
|
|
status = res.getString(R.string.open);
|
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
|
|
- notifyBuilder(status);
|
|
|
- notificationManager.notify(0, notification);
|
|
|
- } else {
|
|
|
- getNotify();
|
|
|
- sendNotification(1);
|
|
|
+ //Se è il service automatico e la finestra principale
|
|
|
+ //non è attiva emmetti la notifica
|
|
|
+ if (isCicleService && !MainActivity.active) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
|
|
+ notifyBuilder(status);
|
|
|
+ notificationManager.notify(0, notification);
|
|
|
+ } else {
|
|
|
+ getNotify();
|
|
|
+ sendNotification(1);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //Salva lo stato dell'applicazione
|
|
|
SUtility.saveState(getApplicationContext(), newStatus,
|
|
|
- last_refresh, statusChange);
|
|
|
+ last_refresh, newChange);
|
|
|
|
|
|
- if (!isCicleService) {
|
|
|
- stopSelf();
|
|
|
- Log.i(LOG_TAG, "Service stop");
|
|
|
- // setDisplay(newStatus, last_refresh, statusChange);
|
|
|
- }
|
|
|
+ //Avvisa la finestra principale di aggiornarsi
|
|
|
+ //o emettere un Toast di sentenza
|
|
|
+ if (isCicleService==false || MainActivity.active)
|
|
|
+ sendMessage(MSG_DONE);
|
|
|
|
|
|
+ //Addormento il service in millisecondi
|
|
|
try {
|
|
|
- Thread.sleep(refreshTime);
|
|
|
+ Log.d(LOG_TAG,"Dormo per: "+refreshTime+" sec");
|
|
|
+ Thread.sleep(refreshTime*1000);
|
|
|
} catch (InterruptedException ie) {
|
|
|
Log.d(LOG_TAG,
|
|
|
"Qualcosa è andato storto con il timer.\n"
|
|
@@ -152,6 +194,10 @@ public class StatusUpdate extends Service {
|
|
|
}
|
|
|
} else {
|
|
|
Log.d(LOG_TAG, "Non c'è connessione");
|
|
|
+ if(! isCicleService){
|
|
|
+ sendMessage(MSG_NO_CONN);
|
|
|
+ stopSelf();
|
|
|
+ }
|
|
|
}
|
|
|
} while (isCicleService);
|
|
|
}
|
|
@@ -159,6 +205,7 @@ public class StatusUpdate extends Service {
|
|
|
|
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ active=true;
|
|
|
isCicleService = intent.getBooleanExtra("isCicleService", false);
|
|
|
if (isCicleService)
|
|
|
refreshTime = intent.getIntExtra("refreshTime", 0);
|
|
@@ -166,19 +213,14 @@ public class StatusUpdate extends Service {
|
|
|
serverName = intent.getStringExtra("serverName");
|
|
|
oldStatus = intent.getBooleanExtra("oldStatus", false);
|
|
|
|
|
|
- Log.i(LOG_TAG, "Service Started");
|
|
|
+ Log.i(LOG_TAG, "Os Service Start Command");
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
+ active = false;
|
|
|
Log.i(LOG_TAG, "Service Destroyed");
|
|
|
}
|
|
|
-
|
|
|
- @Override
|
|
|
- public IBinder onBind(Intent intent) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+}
|