MainActivity.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package net.indivia.hacklabbo.statusreader;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.preference.PreferenceManager;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.widget.ImageButton;
  11. import android.widget.TextView;
  12. public class MainActivity extends ActionBarActivity {
  13. private static final String LOG_TAG = "MainActivity";
  14. private SharedPreferences prefs;
  15. public final static int
  16. MENU_SETTINGS_ID = 10,
  17. MENU_REFRESH_ID = 11;
  18. private static ImageButton ibStatusBtn;
  19. private static TextView
  20. tvStatusValue,tvServerValue,tvLastRefreshValue,tvLastStatusValue;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. prefs = PreferenceManager.getDefaultSharedPreferences(this);
  26. ibStatusBtn = (ImageButton) findViewById(R.id.status_btn);
  27. tvStatusValue = (TextView) findViewById(R.id.status_value);
  28. tvServerValue = (TextView) findViewById(R.id.server_value);
  29. tvLastRefreshValue = (TextView) findViewById(R.id.last_refresh_value);
  30. tvLastStatusValue = (TextView) findViewById(R.id.last_status_change_value);
  31. }
  32. @Override
  33. protected void onResume(){
  34. super.onResume();
  35. LoadPreferences();
  36. }
  37. private void LoadPreferences(){
  38. //le preferenze relative ai servizi
  39. Boolean refresh_service =prefs.getBoolean(getString(R.string.key_refresh_service), false);
  40. Boolean update_service = prefs.getBoolean("update", false);
  41. //setto il display
  42. setDisplay(null,null,null);
  43. //avvio il service che verifica se ci sono update del software
  44. if (update_service)
  45. startService(new Intent(this, CheckUpdate.class));
  46. //avvio il service che verifica se ci sono update dello stato
  47. //TODO: se il service è già avviato?
  48. if (refresh_service)
  49. startService(setUpdateStatusIntent(true));
  50. }
  51. public Intent setUpdateStatusIntent(Boolean isCicleService){
  52. Intent intent = new Intent(this, StatusUpdate.class);
  53. String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE);
  54. int refresh_time = Integer.parseInt(prefs.getString(getString(R.string.key_refresh_status_time), "600"));
  55. intent.putExtra("serverName", serverName);
  56. SharedPreferences states = getApplicationContext().getSharedPreferences(getString(R.string.key_states), 0);
  57. Boolean oldStatus = states.getBoolean(getString(R.string.key_status), false);
  58. intent.putExtra("oldStatus", oldStatus);
  59. intent.putExtra("isCicleService", isCicleService);
  60. if(isCicleService)
  61. intent.putExtra("refreshTime", refresh_time);
  62. return intent;
  63. }
  64. public void setDisplay(Boolean status, String last_refresh, String status_changed){
  65. String serverName = prefs.getString(getString(R.string.key_server_name), StatusUpdate.DEF_SITE);
  66. SharedPreferences states = getApplicationContext().getSharedPreferences(getString(R.string.key_states), 0);
  67. String mai = getString(R.string.key_status_changed);
  68. if(status == null)
  69. status = states.getBoolean(getString(R.string.key_status), false);
  70. if(last_refresh == null)
  71. last_refresh = states.getString(getString(R.string.key_last_refresh), mai);
  72. if(status_changed == null)
  73. status_changed = states.getString(getString(R.string.key_status_changed), mai);
  74. int statusImg = R.drawable.red_btn,
  75. statusValue = R.string.close;
  76. if(status){
  77. statusImg = R.drawable.green_btn;
  78. statusValue = R.string.open;
  79. }
  80. ibStatusBtn.setImageResource(statusImg);
  81. tvStatusValue.setText(statusValue);
  82. tvLastRefreshValue.setText(last_refresh);
  83. tvLastStatusValue.setText(status_changed);
  84. tvServerValue.setText(serverName);
  85. }
  86. @Override
  87. public boolean onCreateOptionsMenu(Menu menu) {
  88. // Inflate the menu; this adds items to the action bar if it is present.
  89. getMenuInflater().inflate(R.menu.main, menu);
  90. return true;
  91. }
  92. @Override
  93. public boolean onOptionsItemSelected(MenuItem item) {
  94. int id = item.getItemId();
  95. if (id == R.id.action_settings) {
  96. startActivity(new Intent(this, PrefScreen.class));
  97. } else if( id== R.id.action_refresh){
  98. startService(setUpdateStatusIntent(false));
  99. }
  100. return super.onOptionsItemSelected(item);
  101. }
  102. @Override
  103. protected void onStop() {
  104. super.onStop();
  105. }
  106. }