00ff98d73a7c7e61807ff72d320fb5a5ae8e2c7c
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / activities / HomeScreen.java
1 package org.netxms.ui.android.main.activities;
2
3 import java.util.ArrayList;
4
5 import org.netxms.base.NXCommon;
6 import org.netxms.client.objects.AbstractObject;
7 import org.netxms.client.objects.GenericObject;
8 import org.netxms.ui.android.R;
9 import org.netxms.ui.android.main.adapters.ActivityListAdapter;
10 import org.netxms.ui.android.main.fragments.AlarmBrowserFragment;
11 import org.netxms.ui.android.service.ClientConnectorService.ConnectionStatus;
12
13 import android.annotation.SuppressLint;
14 import android.content.ComponentName;
15 import android.content.Intent;
16 import android.content.SharedPreferences;
17 import android.content.res.Configuration;
18 import android.os.Build;
19 import android.os.Bundle;
20 import android.os.Handler;
21 import android.os.IBinder;
22 import android.preference.PreferenceManager;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.widget.AdapterView;
27 import android.widget.AdapterView.OnItemClickListener;
28 import android.widget.GridView;
29 import android.widget.TextView;
30 import android.widget.Toast;
31
32 /**
33 * Home screen activity
34 *
35 * @author Victor Kirhenshtein
36 * @author Marco Incalcaterra (marco.incalcaterra@thinksoft.it)
37 *
38 */
39
40 public class HomeScreen extends AbstractClientActivity implements OnItemClickListener
41 {
42 public static final int ACTIVITY_ALARMS = 1;
43 public static final int ACTIVITY_DASHBOARDS = 2;
44 public static final int ACTIVITY_NODES = 3;
45 public static final int ACTIVITY_ENTIRENETWORK = 4;
46 public static final int ACTIVITY_GRAPHS = 5;
47 public static final int ACTIVITY_MACADDRESS = 6;
48 public static final String INTENTIONAL_EXIT_KEY = "IntentionalExit";
49
50 private ActivityListAdapter adapter;
51 private TextView statusText;
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see org.netxms.ui.android.main.activities.AbstractClientActivity#onCreateStep2(android.os.Bundle)
57 */
58 @Override
59 public void onCreateStep2(Bundle savedInstanceState)
60 {
61 setIntentionalExit(false);// Allow autorestart on change connectivity status for premature exit
62 setContentView(R.layout.homescreen);
63
64 GridView gridview = (GridView)findViewById(R.id.ActivityList);
65 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
66 gridview.setNumColumns(5);
67 else
68 gridview.setNumColumns(2);
69 adapter = new ActivityListAdapter(this);
70 gridview.setAdapter(adapter);
71 gridview.setOnItemClickListener(this);
72
73 statusText = (TextView)findViewById(R.id.ScreenTitleSecondary);
74
75 TextView buildName = (TextView)findViewById(R.id.MainScreenVersion);
76 buildName.setText(getString(R.string.version) + " " + NXCommon.VERSION + " (" + getString(R.string.build_number) + ")");
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see org.netxms.ui.android.main.activities.AbstractClientActivity#onServiceConnected(android.content.ComponentName,
83 * android.os.IBinder)
84 */
85 @Override
86 public void onServiceConnected(ComponentName name, IBinder binder)
87 {
88 super.onServiceConnected(name, binder);
89 service.registerHomeScreen(this);
90 setStatusText(service.getConnectionStatusText(), service.getConnectionStatusColor());
91 refreshActivityStatus();
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
98 */
99 @SuppressLint("InlinedApi")
100 @Override
101 public boolean onCreateOptionsMenu(Menu menu)
102 {
103 super.onCreateOptionsMenu(menu);
104 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
105 menu.removeItem(android.R.id.home);
106 menu.add(Menu.NONE, R.string.reconnect, Menu.NONE, getString(R.string.reconnect)).setIcon(android.R.drawable.ic_menu_revert);
107 menu.add(Menu.NONE, R.string.exit, Menu.NONE, getString(R.string.exit)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
108 return true;
109 }
110
111 /*
112 * (non-Javadoc)
113 *
114 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
115 */
116 @Override
117 public boolean onOptionsItemSelected(MenuItem item)
118 {
119 if (item.getItemId() == R.string.reconnect)
120 {
121 if (service != null)
122 service.reconnect(true);
123 return true;
124 }
125 else if (item.getItemId() == R.string.exit)
126 {
127 exit();
128 return true;
129 }
130 return super.onOptionsItemSelected(item);
131 }
132
133 /*
134 * (non-Javadoc)
135 *
136 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
137 */
138 @Override
139 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
140 {
141 // Avoid starting activity if no connection
142 if (service != null && (service.getConnectionStatus() == ConnectionStatus.CS_CONNECTED || service.getConnectionStatus() == ConnectionStatus.CS_ALREADYCONNECTED))
143 switch ((int)id)
144 {
145 case ACTIVITY_ALARMS:
146 startActivity(new Intent(this, AlarmBrowserFragment.class));
147 break;
148 case ACTIVITY_NODES:
149 startActivity(new Intent(this, NodeBrowser.class).putExtra("parentId", GenericObject.SERVICEROOT));
150 break;
151 case ACTIVITY_ENTIRENETWORK:
152 startActivity(new Intent(this, NodeBrowser.class).putExtra("parentId", GenericObject.NETWORK));
153 break;
154 case ACTIVITY_GRAPHS:
155 startActivity(new Intent(this, GraphBrowser.class));
156 break;
157 case ACTIVITY_MACADDRESS:
158 startActivity(new Intent(this, ConnectionPointBrowser.class));
159 break;
160 case ACTIVITY_DASHBOARDS:
161 startActivity(new Intent(this, DashboardBrowser.class));
162 break;
163 default:
164 break;
165 }
166 else
167 showToast(getString(R.string.notify_disconnected));
168 }
169
170 /**
171 * @param text
172 */
173 public void setStatusText(String text, int color)
174 {
175 statusText.setTextColor(color);
176 statusText.setText(text);
177 }
178
179 public void showToast(final String text)
180 {
181 new Handler(getMainLooper()).post(new Runnable()
182 {
183 @Override
184 public void run()
185 {
186 Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
187 }
188 });
189 }
190
191 /**
192 * Exit from app and shutdown service
193 */
194 public void exit()
195 {
196 if (service != null)
197 service.shutdown();
198 setIntentionalExit(true);// Avoid autorestart on change connectivity status for intentional exit
199 moveTaskToBack(true);
200 System.exit(0);
201 }
202
203 /**
204 * Refresh pending status of alarms, called from service
205 */
206 public void refreshPendingAlarms()
207 {
208 adapter.setPendingAlarms(service.getAlarms().length);
209 adapter.notifyDataSetChanged();
210 }
211
212 /**
213 * Refresh the activity status, called from service
214 */
215 public void refreshActivityStatus()
216 {
217 refreshPendingAlarms();
218 ArrayList<AbstractObject> objList = new ArrayList<AbstractObject>();
219 objList.add(service.findObjectById(GenericObject.NETWORK));
220 objList.add(service.findObjectById(GenericObject.SERVICEROOT));
221 objList.add(service.findObjectById(GenericObject.DASHBOARDROOT));
222 adapter.setTopNodes(objList);
223 adapter.notifyDataSetChanged();
224 }
225
226 /**
227 * Set a flag to inform about an intentional exit to avoid
228 * automatic reconnection on change connectivity status
229 *
230 * @param flag true to signal an intentional exit
231 */
232 private void setIntentionalExit(boolean flag)
233 {
234 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
235 SharedPreferences.Editor editor = prefs.edit();
236 editor.putBoolean(INTENTIONAL_EXIT_KEY, flag);
237 editor.commit();
238 }
239 }