9084152a44a9ed9f8b2344c12cdc553ccb57f94f
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / fragments / AlarmsFragment.java
1 /**
2 *
3 */
4 package org.netxms.ui.android.main.fragments;
5
6 import java.util.ArrayList;
7
8 import org.netxms.client.events.Alarm;
9 import org.netxms.ui.android.R;
10 import org.netxms.ui.android.loaders.AlarmLoader;
11 import org.netxms.ui.android.main.adapters.AlarmListAdapter;
12
13 import android.content.SharedPreferences;
14 import android.os.Bundle;
15 import android.preference.PreferenceManager;
16 import android.support.v4.app.LoaderManager;
17 import android.support.v4.content.Loader;
18 import android.util.SparseBooleanArray;
19 import android.view.ContextMenu;
20 import android.view.ContextMenu.ContextMenuInfo;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.AdapterView;
28 import android.widget.AdapterView.AdapterContextMenuInfo;
29 import android.widget.ListView;
30
31 /**
32 * Fragment for alarms info
33 *
34 * @author Marco Incalcaterra (marco.incalcaterra@thinksoft.it)
35 *
36 */
37
38 public class AlarmsFragment extends AbstractListFragment implements LoaderManager.LoaderCallbacks<Alarm[]>
39 {
40 private AlarmListAdapter adapter = null;
41 private AlarmLoader loader = null;
42 private ListView lv = null;
43 private static final String SORT_KEY = "NodeAlarmsSortBy";
44 private final int[] menuSortIds = { R.id.sort_severity_asc, R.id.sort_severity_desc,
45 R.id.sort_date_asc, R.id.sort_date_desc };
46
47 @Override
48 public View onCreateView(LayoutInflater inflater, ViewGroup container,
49 Bundle savedInstanceState)
50 {
51 View v = inflater.inflate(R.layout.alarms_fragment, container, false);
52 createProgress(v);
53 return v;
54 }
55
56 @Override
57 public void onActivityCreated(Bundle savedInstanceState)
58 {
59 super.onActivityCreated(savedInstanceState);
60 adapter = new AlarmListAdapter(getActivity());
61 setListAdapter(adapter);
62 setListShown(false, true);
63 adapter.setService(service);
64 loader = (AlarmLoader)getActivity().getSupportLoaderManager().initLoader(R.layout.alarms_fragment, null, this);
65 if (loader != null)
66 loader.setService(service);
67 lv = getListView();
68 registerForContextMenu(lv);
69 }
70
71 @Override
72 public void refresh()
73 {
74 if (loader != null)
75 loader.setService(service);
76 if (adapter != null)
77 adapter.setService(service);
78 }
79
80 @Override
81 public Loader<Alarm[]> onCreateLoader(int arg0, Bundle arg1)
82 {
83 return new AlarmLoader(getActivity());
84 }
85
86 @Override
87 public void onLoadFinished(Loader<Alarm[]> arg0, Alarm[] arg1)
88 {
89 setListShown(true, true);
90 if (adapter != null)
91 {
92 ArrayList<Integer> id = new ArrayList<Integer>(0);
93 id.add((int)nodeId);
94 adapter.setFilter(id);
95 adapter.setValues(arg1);
96 adapter.notifyDataSetChanged();
97 }
98 }
99
100 @Override
101 public void onLoaderReset(Loader<Alarm[]> arg0)
102 {
103 }
104
105 @Override
106 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
107 {
108 android.view.MenuInflater inflater = getActivity().getMenuInflater();
109 inflater.inflate(R.menu.alarm_actions, menu);
110 menu.removeItem(R.id.viewlastvalues);
111 checkMenuSortItem(menu);
112 }
113
114 @Override
115 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
116 {
117 if (method_invalidateOptionsMenu != null)
118 {
119 inflater.inflate(R.menu.alarm_actions, menu);
120 menu.removeItem(R.id.viewlastvalues);
121 }
122 checkMenuSortItem(menu);
123 super.onCreateOptionsMenu(menu, inflater);
124 }
125
126 @Override
127 public void onPrepareOptionsMenu(Menu menu)
128 {
129 super.onPrepareOptionsMenu(menu);
130 menu.removeItem(R.id.viewlastvalues);
131 if (method_invalidateOptionsMenu == null)
132 {
133 menu.removeItem(R.id.selectall);
134 menu.removeItem(R.id.unselectall);
135 menu.add(Menu.NONE, R.id.selectall, Menu.NONE, getString(R.string.alarm_selectall));
136 menu.add(Menu.NONE, R.id.unselectall, Menu.NONE, getString(R.string.alarm_unselectall));
137 }
138 checkMenuSortItem(menu);
139 }
140
141 @Override
142 public boolean onContextItemSelected(MenuItem item)
143 {
144 if (handleItemSelection(item))
145 return true;
146 return super.onContextItemSelected(item);
147 }
148
149 @Override
150 public boolean onOptionsItemSelected(MenuItem item)
151 {
152 if (handleItemSelection(item))
153 return true;
154 return super.onOptionsItemSelected(item);
155 }
156
157 private void checkMenuSortItem(Menu menu)
158 {
159 int sortBy = PreferenceManager.getDefaultSharedPreferences(getActivity()).getInt(SORT_KEY, 0);
160 MenuItem item = menu.findItem(menuSortIds[sortBy]);
161 if (item != null)
162 item.setChecked(true);
163 }
164
165 private void setNewSort(MenuItem item, int sortBy)
166 {
167 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
168 SharedPreferences.Editor editor = prefs.edit();
169 editor.putInt(SORT_KEY, sortBy);
170 editor.commit();
171 item.setChecked(!item.isChecked());
172 selectAll(false);
173 adapter.setSortBy(sortBy);
174 adapter.sort();
175 adapter.notifyDataSetChanged();
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.sort_severity_asc:
188 setNewSort(item, AlarmListAdapter.SORT_SEVERITY_ASC);
189 return true;
190 case R.id.sort_severity_desc:
191 setNewSort(item, AlarmListAdapter.SORT_SEVERITY_DESC);
192 return true;
193 case R.id.sort_date_asc:
194 setNewSort(item, AlarmListAdapter.SORT_DATE_ASC);
195 return true;
196 case R.id.sort_date_desc:
197 setNewSort(item, AlarmListAdapter.SORT_DATE_DESC);
198 return true;
199 case R.id.sort_name_asc:
200 setNewSort(item, AlarmListAdapter.SORT_NAME_ASC);
201 return true;
202 case R.id.sort_name_desc:
203 setNewSort(item, AlarmListAdapter.SORT_NAME_DESC);
204 return true;
205 case R.id.selectall:
206 selectAll(true);
207 return true;
208 case R.id.unselectall:
209 selectAll(false);
210 return true;
211 case R.id.acknowledge:
212 adapter.doAction(AlarmListAdapter.ACKNOWLEDGE, getAlarmsSelection(item));
213 selectAll(false);
214 return true;
215 case R.id.sticky_acknowledge:
216 adapter.doAction(AlarmListAdapter.STICKY_ACKNOWLEDGE, getAlarmsSelection(item));
217 selectAll(false);
218 return true;
219 case R.id.resolve:
220 adapter.doAction(AlarmListAdapter.RESOLVE, getAlarmsSelection(item));
221 selectAll(false);
222 return true;
223 case R.id.terminate:
224 adapter.doAction(AlarmListAdapter.TERMINATE, getAlarmsSelection(item));
225 selectAll(false);
226 return true;
227 }
228 return false;
229 }
230
231 /**
232 * Get list of selected alarms
233 */
234 private ArrayList<Long> getAlarmsSelection(MenuItem item)
235 {
236 ArrayList<Long> idList = new ArrayList<Long>();
237 final SparseBooleanArray positions = lv.getCheckedItemPositions();
238 if (positions != null && positions.size() > 0)
239 for (int i = 0; i < adapter.getCount(); i++)
240 if (positions.get(i))
241 {
242 Alarm al = (Alarm)adapter.getItem(i);
243 if (al != null)
244 idList.add(al.getId());
245 }
246 if (idList.size() == 0)
247 {
248 AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
249 Alarm al = (Alarm)adapter.getItem(info != null ? info.position : lv.getSelectedItemPosition());
250 if (al != null)
251 idList.add(al.getId());
252 }
253 return idList;
254 }
255
256 private void selectAll(boolean select)
257 {
258 for (int i = 0; i < adapter.getCount(); i++)
259 lv.setItemChecked(i, select);
260 }
261 }