gine 8 years ago
parent
commit
7d5b1eeeb2

+ 2 - 2
AndroidManifest.xml

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="net.indivia.hacklabbo.statusreader"
-    android:versionCode="2"
-    android:versionName="1.0" >
+    android:versionCode="3"
+    android:versionName="1.1" >
     <!-- check per la rete -->
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

+ 2 - 2
bin/AndroidManifest.xml

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="net.indivia.hacklabbo.statusreader"
-    android:versionCode="1"
-    android:versionName="1.0" >
+    android:versionCode="3"
+    android:versionName="1.1" >
     <!-- check per la rete -->
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

BIN
bin/StatusReader.apk


BIN
bin/classes.dex


+ 1 - 1
bin/jarlist.cache

@@ -2,4 +2,4 @@
 # format is <lastModified> <length> <SHA-1> <path>
 # Encoding is UTF-8
 1427838941000 1074044 33b27a8d2a5b87e6eb6c60e87a5374ffbfeacc3a /weight/Code/appcompat_v7/libs/android-support-v4.jar
-1427839433000 1074044 33b27a8d2a5b87e6eb6c60e87a5374ffbfeacc3a /weight/Code/StatusReader/libs/android-support-v4.jar
+1428604187000 1074044 33b27a8d2a5b87e6eb6c60e87a5374ffbfeacc3a /weight/Code/StatusReader/libs/android-support-v4.jar

BIN
bin/resources.ap_


+ 2 - 0
res/values/prefs_arrays.xml

@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string-array name="prefs_refresh_time">
+        <item>30 sec (debug)</item>
         <item>3 minuti</item>
         <item>5 minuti</item>
         <item>10 minuti</item>
@@ -8,6 +9,7 @@
         <item>30 minuti</item>
     </string-array>
     <string-array name="prefs_refresh_time_value">
+        <item>30</item>
         <item>180</item>
 		<item>300</item>
         <item>600</item>

+ 1 - 1
res/xml/prefs.xml

@@ -26,7 +26,7 @@
 	        android:summary="@string/prefs_server_name_summary"
 	        android:key="@string/key_server_name"
 	        android:persistent="true"
-	        android:defaultValue="isopen.hacklabbo.indivia.net/hacklabbo/state.json"
+	        android:defaultValue="http://isopen.hacklabbo.indivia.net/hacklabbo/state.json"
 	        />
 		<CheckBoxPreference 
             android:title="@string/prefs_update_title" 

+ 0 - 1
src/net/indivia/hacklabbo/statusreader/CheckUpdate.java

@@ -9,7 +9,6 @@ import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
-import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
 import android.net.Uri;

+ 65 - 13
src/net/indivia/hacklabbo/statusreader/MainActivity.java

@@ -1,7 +1,11 @@
 package net.indivia.hacklabbo.statusreader;
 
+import android.support.v4.content.LocalBroadcastManager;
 import android.support.v7.app.ActionBarActivity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
@@ -10,18 +14,45 @@ import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.ImageButton;
 import android.widget.TextView;
+import android.widget.Toast;
 
 public class MainActivity extends ActionBarActivity {
+	public static boolean active = false;
 	private static final String LOG_TAG = "MainActivity";
-	private SharedPreferences prefs;
+	private static SharedPreferences prefs;
+	private Intent statusIntent;
 	public final static int 
 		MENU_SETTINGS_ID = 10,
 		MENU_REFRESH_ID = 11;
 	private static ImageButton ibStatusBtn;
 	private static TextView 
 		tvStatusValue,tvServerValue,tvLastRefreshValue,tvLastStatusValue;
-	
 
+	// Our handler for received Intents. This will be called whenever an Intent
+	// with an action named "custom-event-name" is broadcasted.
+	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
+		  @Override
+		  public void onReceive(Context context, Intent intent) {
+			  String message = intent.getStringExtra("msg");
+			  Boolean isCicleService = intent.getBooleanExtra("isCicleService",false);
+			  if(! message.equalsIgnoreCase(""))
+			  	Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
+			  	
+			  setDisplay(null,null,null);
+			  
+			  Boolean refresh_service =prefs.getBoolean(getString(R.string.key_refresh_service), false);
+			  Log.d(LOG_TAG,"refresh:"+refresh_service+" cicle:"+isCicleService);
+			  if(isCicleService==false){
+				  Log.d(LOG_TAG,"stop service");
+				  stopService(statusIntent);
+				  if (refresh_service){
+					  Log.d(LOG_TAG,"restart service");
+					  startService(setUpdateStatusIntent(statusIntent, true));
+				  }
+			  }
+		  }
+	};
+	
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
@@ -33,11 +64,18 @@ public class MainActivity extends ActionBarActivity {
 		tvServerValue = (TextView) findViewById(R.id.server_value);
 		tvLastRefreshValue = (TextView) findViewById(R.id.last_refresh_value);
 		tvLastStatusValue = (TextView) findViewById(R.id.last_status_change_value);
+		
+		statusIntent = new Intent(this, StatusUpdate.class);
+		LocalBroadcastManager.getInstance(this).registerReceiver(
+				mMessageReceiver,
+			    new IntentFilter("filter-updated"));
 	}
 	
 	@Override
 	protected void onResume(){
 		super.onResume();
+		
+		active = true;
 		LoadPreferences();
 	}
 	
@@ -52,16 +90,16 @@ public class MainActivity extends ActionBarActivity {
 	    //avvio il service che verifica se ci sono update del software
 		if (update_service)
 			startService(new Intent(this, CheckUpdate.class));
-			
+		
+		
 		//avvio il service che verifica se ci sono update dello stato
-		//TODO: se il service è già avviato?
-		if (refresh_service)
-			startService(setUpdateStatusIntent(true));
+		if (refresh_service){
+			stopService(statusIntent);
+			startService(setUpdateStatusIntent(statusIntent,true));
+		}
 	}
 	
-	public Intent setUpdateStatusIntent(Boolean isCicleService){
-		Intent intent = new Intent(this, StatusUpdate.class);
-		
+	public Intent setUpdateStatusIntent(Intent intent, Boolean isCicleService){
 		String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE);
 		int refresh_time =  Integer.parseInt(prefs.getString(getString(R.string.key_refresh_status_time), "600"));
 		
@@ -97,6 +135,10 @@ public class MainActivity extends ActionBarActivity {
 	    	statusImg = R.drawable.green_btn;
 	    	statusValue = R.string.open;
 	    }
+	        
+	    Log.d(LOG_TAG, "status_value: "+statusValue+" last_refresh: "+last_refresh+
+	    		" status_changed: "+status_changed+" server_name: "+serverName);
+	    
 	    ibStatusBtn.setImageResource(statusImg);
     	tvStatusValue.setText(statusValue);
 	    
@@ -118,13 +160,23 @@ public class MainActivity extends ActionBarActivity {
 		if (id == R.id.action_settings) {
 			startActivity(new Intent(this, PrefScreen.class));
 		} else if( id== R.id.action_refresh){
-			startService(setUpdateStatusIntent(false));
+			stopService(statusIntent);
+			startService(setUpdateStatusIntent(statusIntent, false));
 		}
 		return super.onOptionsItemSelected(item);
 	}
 	
 	@Override
-    protected void onStop() {
-      super.onStop();
-    }
+	protected void onPause(){
+		active=false;
+		super.onPause();
+	}
+	
+	@Override
+	protected void onDestroy() {
+	  // Unregister since the activity is about to be closed.
+	  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
+	  super.onDestroy();
+	}
+	
 }

+ 0 - 34
src/net/indivia/hacklabbo/statusreader/PrefScreen.java

@@ -1,25 +1,14 @@
 package net.indivia.hacklabbo.statusreader;
 
-import android.app.Dialog;
 import android.content.Context;
-import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
-import android.net.Uri;
 import android.os.Bundle;
-import android.preference.Preference;
-import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceManager;
-import android.util.Log;
-import android.widget.Toast;
 
 public class PrefScreen extends PreferenceActivity {
 	private static final String LOG_TAG = "PreferenceScreenActivity";
-	private static String 
-		SPREF_KEY_SERV,
-		SPREF_KEY_REFRESH,
-		SPREF_KEY_UPDATE;
 	private static Context ctx;
 
     @Override
@@ -30,10 +19,6 @@ public class PrefScreen extends PreferenceActivity {
         
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
         prefs.registerOnSharedPreferenceChangeListener(mListener);
-        
-		/*SPREF_KEY_SERV = getString(R.string.key_refresh_service);
-		SPREF_KEY_REFRESH = getString(R.string.key_refresh_time);
-		SPREF_KEY_UPDATE = getString(R.string.key_update_service);*/
     }
 
 	private void extracted() {
@@ -43,25 +28,6 @@ public class PrefScreen extends PreferenceActivity {
     public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {        
         @Override
         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
-        	boolean preference;
-        	Log.d(LOG_TAG, "onSharedPrefCh(): "+key);
-        	/*if (key.equalsIgnoreCase(SPREF_KEY_SERV)){
-        		preference = sharedPreferences.getBoolean(SPREF_KEY_SERV, false);
-        		if(preference){
-        			startService(ATab.feedBurnI);
-        		} else {
-        			stopService(ATab.feedBurnI);
-        		}
-        	} else if (key.equalsIgnoreCase(SPREF_KEY_REFRESH)) {
-        		stopService(ATab.feedBurnI);
-        		startService(ATab.feedBurnI);
-        	}*/
-        	
-        	/*preference = sharedPreferences.getBoolean(SPREF_KEY_UPDATE, true);
-        	if (preference){
-                Intent iUpdate = new Intent(ctx,CheckUpdate.class);
-        		startService(iUpdate);
-        	}*/
         }
     };
 }

+ 1 - 1
src/net/indivia/hacklabbo/statusreader/SNetwork.java

@@ -73,7 +73,7 @@ public class SNetwork {
 				e1.printStackTrace();
 			}
 		} catch (MalformedURLException e2) {
-			Log.d(LOG_TAG,"mal url");
+			Log.d(LOG_TAG,"Url non idoneo:"+ uri);
 			result = "Manca il protocollo";
 			e2.printStackTrace();
 		} catch (IOException e2) {

+ 3 - 2
src/net/indivia/hacklabbo/statusreader/SUtility.java

@@ -25,10 +25,11 @@ public class SUtility {
 	    editor.putString(res.getString(R.string.key_last_refresh), last_refresh);
 	    editor.putString(res.getString(R.string.key_status_changed), status_change);
 	    
-	    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO)
+	    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO){
 	    	editor.apply();
-	    else
+	    } else {
 	    	commitShared(editor);
+	    }
 	}
 	
 	@TargetApi(Build.VERSION_CODES.FROYO)

+ 78 - 36
src/net/indivia/hacklabbo/statusreader/StatusUpdate.java

@@ -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;
-	}
-
-}
+}