adbfde145c54607b11ebc1623f489d960682f1b8
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / fragments / LastValuesFragment.java
1 /**
2 *
3 */
4 package org.netxms.ui.android.main.fragments;
5
6 import java.util.ArrayList;
7
8 import org.netxms.client.datacollection.DataCollectionObject;
9 import org.netxms.client.datacollection.DciValue;
10 import org.netxms.ui.android.R;
11 import org.netxms.ui.android.loaders.DciValueLoader;
12 import org.netxms.ui.android.main.activities.DrawBarChart;
13 import org.netxms.ui.android.main.activities.DrawGraph;
14 import org.netxms.ui.android.main.activities.DrawPieChart;
15 import org.netxms.ui.android.main.activities.TableLastValues;
16 import org.netxms.ui.android.main.adapters.LastValuesAdapter;
17
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.support.v4.app.LoaderManager;
21 import android.support.v4.content.Loader;
22 import android.util.SparseBooleanArray;
23 import android.view.ContextMenu;
24 import android.view.ContextMenu.ContextMenuInfo;
25 import android.view.LayoutInflater;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.AdapterView;
32 import android.widget.AdapterView.AdapterContextMenuInfo;
33 import android.widget.ListView;
34
35 /**
36 * Fragment for last values info
37 *
38 * @author Marco Incalcaterra (marco.incalcaterra@thinksoft.it)
39 *
40 */
41
42 public class LastValuesFragment extends AbstractListFragment implements LoaderManager.LoaderCallbacks<DciValue[]>
43 {
44 private LastValuesAdapter adapter = null;
45 private DciValueLoader loader = null;
46 private ListView lv = null;
47
48 private static final Integer[] DEFAULT_COLORS = { 0x40699C, 0x9E413E, 0x7F9A48, 0x695185, 0x3C8DA3, 0xCC7B38, 0x4F81BD, 0xC0504D,
49 0x9BBB59, 0x8064A2, 0x4BACC6, 0xF79646, 0xAABAD7, 0xD9AAA9, 0xC6D6AC, 0xBAB0C9 };
50 private static final int MAX_COLORS = DEFAULT_COLORS.length;
51
52 @Override
53 public View onCreateView(LayoutInflater inflater, ViewGroup container,
54 Bundle savedInstanceState)
55 {
56 View v = inflater.inflate(R.layout.lastvalues_fragment, container, false);
57 createProgress(v);
58 return v;
59 }
60
61 @Override
62 public void onActivityCreated(Bundle savedInstanceState)
63 {
64 super.onActivityCreated(savedInstanceState);
65 adapter = new LastValuesAdapter(getActivity().getApplicationContext());
66 setListAdapter(adapter);
67 setListShown(false, true);
68 loader = (DciValueLoader)getActivity().getSupportLoaderManager().initLoader(R.layout.lastvalues_fragment, null, this);
69 if (loader != null)
70 {
71 loader.setObjId(nodeId);
72 loader.setService(service);
73 }
74 lv = getListView();
75 registerForContextMenu(lv);
76 }
77
78 @Override
79 public void refresh()
80 {
81 if (loader != null)
82 {
83 loader.setObjId(nodeId);
84 loader.setService(service);
85 }
86 }
87
88 @Override
89 public Loader<DciValue[]> onCreateLoader(int arg0, Bundle arg1)
90 {
91 return new DciValueLoader(getActivity().getApplicationContext());
92 }
93
94 @Override
95 public void onLoadFinished(Loader<DciValue[]> arg0, DciValue[] arg1)
96 {
97 setListShown(true, true);
98 if (adapter != null)
99 {
100 adapter.setValues(arg1);
101 adapter.notifyDataSetChanged();
102 }
103 }
104
105 @Override
106 public void onLoaderReset(Loader<DciValue[]> arg0)
107 {
108 }
109
110 @Override
111 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
112 {
113 android.view.MenuInflater inflater = getActivity().getMenuInflater();
114 DciValue value = (DciValue)adapter.getItem(((AdapterView.AdapterContextMenuInfo)menuInfo).position);
115 if (value != null)
116 {
117 switch (value.getDcObjectType())
118 {
119 case DataCollectionObject.DCO_TYPE_ITEM:
120 inflater.inflate(R.menu.last_values_actions, menu);
121 break;
122 case DataCollectionObject.DCO_TYPE_TABLE:
123 inflater.inflate(R.menu.last_values_table_actions, menu);
124 break;
125 }
126 }
127 }
128
129 @Override
130 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
131 {
132 if (method_invalidateOptionsMenu != null)
133 inflater.inflate(R.menu.last_values_actions, menu);
134 super.onCreateOptionsMenu(menu, inflater);
135 }
136
137 @Override
138 public void onPrepareOptionsMenu(Menu menu)
139 {
140 super.onPrepareOptionsMenu(menu);
141 if (method_invalidateOptionsMenu == null)
142 {
143 menu.removeItem(R.id.graph_half_hour);
144 menu.removeItem(R.id.graph_one_hour);
145 menu.removeItem(R.id.graph_two_hours);
146 menu.removeItem(R.id.graph_four_hours);
147 menu.removeItem(R.id.graph_one_day);
148 menu.removeItem(R.id.graph_one_week);
149 menu.removeItem(R.id.bar_chart);
150 menu.removeItem(R.id.pie_chart);
151 menu.add(Menu.NONE, R.id.graph_half_hour, Menu.NONE, getString(R.string.last_values_graph_half_hour));
152 menu.add(Menu.NONE, R.id.graph_one_hour, Menu.NONE, getString(R.string.last_values_graph_one_hour));// .setIcon(R.drawable.ic_menu_line_chart);
153 menu.add(Menu.NONE, R.id.graph_two_hours, Menu.NONE, getString(R.string.last_values_graph_two_hours));
154 menu.add(Menu.NONE, R.id.graph_four_hours, Menu.NONE, getString(R.string.last_values_graph_four_hours));
155 menu.add(Menu.NONE, R.id.graph_one_day, Menu.NONE, getString(R.string.last_values_graph_one_day));
156 menu.add(Menu.NONE, R.id.graph_one_week, Menu.NONE, getString(R.string.last_values_graph_one_week));
157 menu.add(Menu.NONE, R.id.bar_chart, Menu.NONE, getString(R.string.last_values_bar_chart));
158 menu.add(Menu.NONE, R.id.pie_chart, Menu.NONE, getString(R.string.last_values_pie_chart));
159 }
160 }
161
162 @Override
163 public boolean onContextItemSelected(MenuItem item)
164 {
165 if (handleItemSelection(item))
166 return true;
167 return super.onContextItemSelected(item);
168 }
169
170 @Override
171 public boolean onOptionsItemSelected(MenuItem item)
172 {
173 if (handleItemSelection(item))
174 return true;
175 return super.onOptionsItemSelected(item);
176 }
177
178 /**
179 * Handles menu item selection for both Option and Context menus
180 * @param item Menu item to handle
181 * @return true if menu has been properly handled
182 */
183 private boolean handleItemSelection(MenuItem item)
184 {
185 switch (item.getItemId())
186 {
187 case R.id.table_last_value:
188 return showTableLastValue(getLastValuesSelection(item));
189 case R.id.graph_half_hour:
190 return drawGraph(1800, getLastValuesSelection(item));
191 case R.id.graph_one_hour:
192 return drawGraph(3600, getLastValuesSelection(item));
193 case R.id.graph_two_hours:
194 return drawGraph(7200, getLastValuesSelection(item));
195 case R.id.graph_four_hours:
196 return drawGraph(14400, getLastValuesSelection(item));
197 case R.id.graph_one_day:
198 return drawGraph(86400, getLastValuesSelection(item));
199 case R.id.graph_one_week:
200 return drawGraph(604800, getLastValuesSelection(item));
201 case R.id.bar_chart:
202 return drawComparisonChart(DrawBarChart.class);
203 case R.id.pie_chart:
204 return drawComparisonChart(DrawPieChart.class);
205 }
206 return false;
207 }
208
209 /**
210 * Get list of selected dci values
211 */
212 private ArrayList<Long> getLastValuesSelection(MenuItem item)
213 {
214 ArrayList<Long> idList = new ArrayList<Long>();
215 final SparseBooleanArray positions = lv.getCheckedItemPositions();
216 if (positions != null && positions.size() > 0)
217 for (int i = 0; i < adapter.getCount(); i++)
218 if (positions.get(i))
219 idList.add((long)i);
220 if (idList.size() == 0)
221 {
222 AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
223 idList.add((long)(info != null ? info.position : lv.getSelectedItemPosition()));
224 }
225 return idList;
226 }
227
228 /**
229 * Show last value for table DCI
230 *
231 * @param position
232 * @return
233 */
234 private boolean showTableLastValue(ArrayList<Long> idList)
235 {
236 if (idList.size() > 0)
237 {
238 DciValue value = (DciValue)adapter.getItem(idList.get(0).intValue());
239 Intent newIntent = new Intent(getActivity(), TableLastValues.class);
240 newIntent.putExtra("nodeId", (int)nodeId);
241 newIntent.putExtra("dciId", (int)value.getId());
242 newIntent.putExtra("description", value.getDescription());
243 startActivity(newIntent);
244 }
245 return true;
246 }
247
248 /**
249 * @param secsBack
250 * @param val
251 * @return
252 */
253 private boolean drawGraph(long secsBack, ArrayList<Long> idList)
254 {
255 if (idList.size() > 0)
256 {
257 ArrayList<Integer> nodeIdList = new ArrayList<Integer>();
258 ArrayList<Integer> dciIdList = new ArrayList<Integer>();
259 ArrayList<Integer> colorList = new ArrayList<Integer>();
260 ArrayList<Integer> lineWidthList = new ArrayList<Integer>();
261 ArrayList<String> nameList = new ArrayList<String>();
262
263 // Set values
264 int count = 0;
265 for (int i = 0; i < idList.size() && count < MAX_COLORS; i++)
266 {
267 DciValue value = (DciValue)adapter.getItem(idList.get(i).intValue());
268 if (value != null && value.getDcObjectType() == DataCollectionObject.DCO_TYPE_ITEM)
269 {
270 nodeIdList.add((int)nodeId);
271 dciIdList.add((int)value.getId());
272 colorList.add(DEFAULT_COLORS[count]);
273 lineWidthList.add(3);
274 nameList.add(value.getDescription());
275 count++;
276 }
277 }
278
279 // Pass them to activity
280 if (count > 0)
281 {
282 Intent newIntent = new Intent(getActivity(), DrawGraph.class);
283 if (count == 1)
284 newIntent.putExtra("graphTitle", nameList.get(0));
285 newIntent.putExtra("numGraphs", count);
286 newIntent.putIntegerArrayListExtra("nodeIdList", nodeIdList);
287 newIntent.putIntegerArrayListExtra("dciIdList", dciIdList);
288 newIntent.putIntegerArrayListExtra("colorList", colorList);
289 newIntent.putIntegerArrayListExtra("lineWidthList", lineWidthList);
290 newIntent.putStringArrayListExtra("nameList", nameList);
291 newIntent.putExtra("timeFrom", System.currentTimeMillis() - secsBack * 1000);
292 newIntent.putExtra("timeTo", System.currentTimeMillis());
293 startActivity(newIntent);
294 }
295 }
296 return true;
297 }
298
299 /**
300 * Draw pie chart for selected DCIs
301 *
302 * @return
303 */
304 private boolean drawComparisonChart(Class<?> chartClass)
305 {
306 Intent newIntent = new Intent(getActivity(), chartClass);
307
308 ArrayList<Integer> nodeIdList = new ArrayList<Integer>();
309 ArrayList<Integer> dciIdList = new ArrayList<Integer>();
310 ArrayList<Integer> colorList = new ArrayList<Integer>();
311 ArrayList<String> nameList = new ArrayList<String>();
312
313 // Set values
314 int count = 0;
315 final SparseBooleanArray positions = lv.getCheckedItemPositions();
316 if (positions.size() > 0)
317 {
318 for (int i = 0; (i < adapter.getCount()) && (count < MAX_COLORS); i++)
319 {
320 if (!positions.get(i))
321 continue;
322
323 DciValue value = (DciValue)adapter.getItem(i);
324 if (value.getDcObjectType() != DataCollectionObject.DCO_TYPE_ITEM)
325 continue;
326
327 nodeIdList.add((int)nodeId);
328 dciIdList.add((int)value.getId());
329 colorList.add(DEFAULT_COLORS[count]);
330 nameList.add(value.getDescription());
331 count++;
332 }
333 }
334
335 // Pass them to activity
336 if (count > 0)
337 {
338 newIntent.putExtra("numItems", count);
339 newIntent.putIntegerArrayListExtra("nodeIdList", nodeIdList);
340 newIntent.putIntegerArrayListExtra("dciIdList", dciIdList);
341 newIntent.putIntegerArrayListExtra("colorList", colorList);
342 newIntent.putStringArrayListExtra("nameList", nameList);
343 startActivity(newIntent);
344 }
345 return true;
346 }
347 }