82a1ce13acc766394cc6a46c1d5edcaf4be01f4e
[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 private static final Integer[] DEFAULT_COLORS = { 0x40699C, 0x9E413E, 0x7F9A48, 0x695185, 0x3C8DA3, 0xCC7B38, 0x4F81BD, 0xC0504D,
48 0x9BBB59, 0x8064A2, 0x4BACC6, 0xF79646, 0xAABAD7, 0xD9AAA9, 0xC6D6AC, 0xBAB0C9 };
49 private static final int MAX_COLORS = DEFAULT_COLORS.length;
50
51 @Override
52 public View onCreateView(LayoutInflater inflater, ViewGroup container,
53 Bundle savedInstanceState)
54 {
55 View v = inflater.inflate(R.layout.lastvalues_fragment, container, false);
56 createProgress(v);
57 return v;
58 }
59
60 @Override
61 public void onActivityCreated(Bundle savedInstanceState)
62 {
63 super.onActivityCreated(savedInstanceState);
64 adapter = new LastValuesAdapter(getActivity().getApplicationContext());
65 setListAdapter(adapter);
66 setListShown(false, true);
67 loader = (DciValueLoader)getActivity().getSupportLoaderManager().initLoader(R.layout.lastvalues_fragment, null, this);
68 if (loader != null)
69 {
70 loader.setObjId(nodeId);
71 loader.setService(service);
72 }
73 lv = getListView();
74 registerForContextMenu(lv);
75 }
76
77 @Override
78 public void refresh()
79 {
80 if (loader != null)
81 {
82 loader.setObjId(nodeId);
83 loader.setService(service);
84 loader.forceLoad();
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 inflater.inflate(R.menu.last_values_actions, menu);
133 super.onCreateOptionsMenu(menu, inflater);
134 }
135
136 @Override
137 public void onPrepareOptionsMenu(Menu menu)
138 {
139 super.onPrepareOptionsMenu(menu);
140 }
141
142 @Override
143 public boolean onContextItemSelected(MenuItem item)
144 {
145 if (handleItemSelection(item))
146 return true;
147 return super.onContextItemSelected(item);
148 }
149
150 @Override
151 public boolean onOptionsItemSelected(MenuItem item)
152 {
153 if (handleItemSelection(item))
154 return true;
155 return super.onOptionsItemSelected(item);
156 }
157
158 /**
159 * Handles menu item selection for both Option and Context menus
160 * @param item Menu item to handle
161 * @return true if menu has been properly handled
162 */
163 private boolean handleItemSelection(MenuItem item)
164 {
165 switch (item.getItemId())
166 {
167 case R.id.table_last_value:
168 return showTableLastValue(getLastValuesSelection(item));
169 case R.id.graph_half_hour:
170 return drawGraph(1800, getLastValuesSelection(item));
171 case R.id.graph_one_hour:
172 return drawGraph(3600, getLastValuesSelection(item));
173 case R.id.graph_two_hours:
174 return drawGraph(7200, getLastValuesSelection(item));
175 case R.id.graph_four_hours:
176 return drawGraph(14400, getLastValuesSelection(item));
177 case R.id.graph_one_day:
178 return drawGraph(86400, getLastValuesSelection(item));
179 case R.id.graph_one_week:
180 return drawGraph(604800, getLastValuesSelection(item));
181 case R.id.bar_chart:
182 return drawComparisonChart(DrawBarChart.class);
183 case R.id.pie_chart:
184 return drawComparisonChart(DrawPieChart.class);
185 }
186 return false;
187 }
188
189 /**
190 * Get list of selected dci values
191 */
192 private ArrayList<Long> getLastValuesSelection(MenuItem item)
193 {
194 ArrayList<Long> idList = new ArrayList<Long>();
195 final SparseBooleanArray positions = lv.getCheckedItemPositions();
196 if (positions != null && positions.size() > 0)
197 for (int i = 0; i < adapter.getCount(); i++)
198 if (positions.get(i))
199 idList.add((long)i);
200 if (idList.size() == 0)
201 {
202 AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
203 idList.add((long)(info != null ? info.position : lv.getSelectedItemPosition()));
204 }
205 return idList;
206 }
207
208 /**
209 * Show last value for table DCI
210 *
211 * @param position
212 * @return
213 */
214 private boolean showTableLastValue(ArrayList<Long> idList)
215 {
216 if (idList.size() > 0)
217 {
218 DciValue value = (DciValue)adapter.getItem(idList.get(0).intValue());
219 Intent newIntent = new Intent(getActivity(), TableLastValues.class);
220 newIntent.putExtra("nodeId", (int)nodeId);
221 newIntent.putExtra("dciId", (int)value.getId());
222 newIntent.putExtra("description", value.getDescription());
223 startActivity(newIntent);
224 }
225 return true;
226 }
227
228 /**
229 * @param secsBack
230 * @param val
231 * @return
232 */
233 private boolean drawGraph(long secsBack, ArrayList<Long> idList)
234 {
235 if (idList.size() > 0)
236 {
237 ArrayList<Integer> nodeIdList = new ArrayList<Integer>();
238 ArrayList<Integer> dciIdList = new ArrayList<Integer>();
239 ArrayList<Integer> colorList = new ArrayList<Integer>();
240 ArrayList<Integer> lineWidthList = new ArrayList<Integer>();
241 ArrayList<String> nameList = new ArrayList<String>();
242
243 // Set values
244 int count = 0;
245 for (int i = 0; i < idList.size() && count < MAX_COLORS; i++)
246 {
247 DciValue value = (DciValue)adapter.getItem(idList.get(i).intValue());
248 if (value != null && value.getDcObjectType() == DataCollectionObject.DCO_TYPE_ITEM)
249 {
250 nodeIdList.add((int)nodeId);
251 dciIdList.add((int)value.getId());
252 colorList.add(DEFAULT_COLORS[count]);
253 lineWidthList.add(3);
254 nameList.add(value.getDescription());
255 count++;
256 }
257 }
258
259 // Pass them to activity
260 if (count > 0)
261 {
262 Intent newIntent = new Intent(getActivity(), DrawGraph.class);
263 if (count == 1)
264 newIntent.putExtra("graphTitle", nameList.get(0));
265 newIntent.putExtra("numGraphs", count);
266 newIntent.putIntegerArrayListExtra("nodeIdList", nodeIdList);
267 newIntent.putIntegerArrayListExtra("dciIdList", dciIdList);
268 newIntent.putIntegerArrayListExtra("colorList", colorList);
269 newIntent.putIntegerArrayListExtra("lineWidthList", lineWidthList);
270 newIntent.putStringArrayListExtra("nameList", nameList);
271 newIntent.putExtra("timeFrom", System.currentTimeMillis() - secsBack * 1000);
272 newIntent.putExtra("timeTo", System.currentTimeMillis());
273 startActivity(newIntent);
274 }
275 }
276 return true;
277 }
278
279 /**
280 * Draw pie chart for selected DCIs
281 *
282 * @return
283 */
284 private boolean drawComparisonChart(Class<?> chartClass)
285 {
286 Intent newIntent = new Intent(getActivity(), chartClass);
287
288 ArrayList<Integer> nodeIdList = new ArrayList<Integer>();
289 ArrayList<Integer> dciIdList = new ArrayList<Integer>();
290 ArrayList<Integer> colorList = new ArrayList<Integer>();
291 ArrayList<String> nameList = new ArrayList<String>();
292
293 // Set values
294 int count = 0;
295 final SparseBooleanArray positions = lv.getCheckedItemPositions();
296 if (positions.size() > 0)
297 {
298 for (int i = 0; (i < adapter.getCount()) && (count < MAX_COLORS); i++)
299 {
300 if (!positions.get(i))
301 continue;
302
303 DciValue value = (DciValue)adapter.getItem(i);
304 if (value.getDcObjectType() != DataCollectionObject.DCO_TYPE_ITEM)
305 continue;
306
307 nodeIdList.add((int)nodeId);
308 dciIdList.add((int)value.getId());
309 colorList.add(DEFAULT_COLORS[count]);
310 nameList.add(value.getDescription());
311 count++;
312 }
313 }
314
315 // Pass them to activity
316 if (count > 0)
317 {
318 newIntent.putExtra("numItems", count);
319 newIntent.putIntegerArrayListExtra("nodeIdList", nodeIdList);
320 newIntent.putIntegerArrayListExtra("dciIdList", dciIdList);
321 newIntent.putIntegerArrayListExtra("colorList", colorList);
322 newIntent.putStringArrayListExtra("nameList", nameList);
323 startActivity(newIntent);
324 }
325 return true;
326 }
327 }