This commit is contained in:
gine 2015-07-08 00:49:44 +02:00
parent 6096b5e34c
commit 7d5b1eeeb2
14 changed files with 155 additions and 93 deletions

View file

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

View file

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

Binary file not shown.

Binary file not shown.

View file

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

Binary file not shown.

View file

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

View file

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

View file

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

View file

@ -1,7 +1,11 @@
package net.indivia.hacklabbo.statusreader; package net.indivia.hacklabbo.statusreader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -10,18 +14,45 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity { public class MainActivity extends ActionBarActivity {
public static boolean active = false;
private static final String LOG_TAG = "MainActivity"; private static final String LOG_TAG = "MainActivity";
private SharedPreferences prefs; private static SharedPreferences prefs;
private Intent statusIntent;
public final static int public final static int
MENU_SETTINGS_ID = 10, MENU_SETTINGS_ID = 10,
MENU_REFRESH_ID = 11; MENU_REFRESH_ID = 11;
private static ImageButton ibStatusBtn; private static ImageButton ibStatusBtn;
private static TextView private static TextView
tvStatusValue,tvServerValue,tvLastRefreshValue,tvLastStatusValue; 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -33,11 +64,18 @@ public class MainActivity extends ActionBarActivity {
tvServerValue = (TextView) findViewById(R.id.server_value); tvServerValue = (TextView) findViewById(R.id.server_value);
tvLastRefreshValue = (TextView) findViewById(R.id.last_refresh_value); tvLastRefreshValue = (TextView) findViewById(R.id.last_refresh_value);
tvLastStatusValue = (TextView) findViewById(R.id.last_status_change_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 @Override
protected void onResume(){ protected void onResume(){
super.onResume(); super.onResume();
active = true;
LoadPreferences(); LoadPreferences();
} }
@ -52,16 +90,16 @@ public class MainActivity extends ActionBarActivity {
//avvio il service che verifica se ci sono update del software //avvio il service che verifica se ci sono update del software
if (update_service) if (update_service)
startService(new Intent(this, CheckUpdate.class)); startService(new Intent(this, CheckUpdate.class));
//avvio il service che verifica se ci sono update dello stato //avvio il service che verifica se ci sono update dello stato
//TODO: se il service è già avviato? if (refresh_service){
if (refresh_service) stopService(statusIntent);
startService(setUpdateStatusIntent(true)); startService(setUpdateStatusIntent(statusIntent,true));
}
} }
public Intent setUpdateStatusIntent(Boolean isCicleService){ public Intent setUpdateStatusIntent(Intent intent, Boolean isCicleService){
Intent intent = new Intent(this, StatusUpdate.class);
String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE); 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")); 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; statusImg = R.drawable.green_btn;
statusValue = R.string.open; 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); ibStatusBtn.setImageResource(statusImg);
tvStatusValue.setText(statusValue); tvStatusValue.setText(statusValue);
@ -118,13 +160,23 @@ public class MainActivity extends ActionBarActivity {
if (id == R.id.action_settings) { if (id == R.id.action_settings) {
startActivity(new Intent(this, PrefScreen.class)); startActivity(new Intent(this, PrefScreen.class));
} else if( id== R.id.action_refresh){ } else if( id== R.id.action_refresh){
startService(setUpdateStatusIntent(false)); stopService(statusIntent);
startService(setUpdateStatusIntent(statusIntent, false));
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override @Override
protected void onStop() { protected void onPause(){
super.onStop(); active=false;
} super.onPause();
}
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
} }

View file

@ -1,25 +1,14 @@
package net.indivia.hacklabbo.statusreader; package net.indivia.hacklabbo.statusreader;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
public class PrefScreen extends PreferenceActivity { public class PrefScreen extends PreferenceActivity {
private static final String LOG_TAG = "PreferenceScreenActivity"; private static final String LOG_TAG = "PreferenceScreenActivity";
private static String
SPREF_KEY_SERV,
SPREF_KEY_REFRESH,
SPREF_KEY_UPDATE;
private static Context ctx; private static Context ctx;
@Override @Override
@ -30,10 +19,6 @@ public class PrefScreen extends PreferenceActivity {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
prefs.registerOnSharedPreferenceChangeListener(mListener); 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() { private void extracted() {
@ -43,25 +28,6 @@ public class PrefScreen extends PreferenceActivity {
public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() { public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 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);
}*/
} }
}; };
} }

View file

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

View file

@ -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_last_refresh), last_refresh);
editor.putString(res.getString(R.string.key_status_changed), status_change); 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(); editor.apply();
else } else {
commitShared(editor); commitShared(editor);
}
} }
@TargetApi(Build.VERSION_CODES.FROYO) @TargetApi(Build.VERSION_CODES.FROYO)

View file

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