3a6f327725c7ac105bedb59e661e2856ab6b5622
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / fragments / AbstractListFragment.java
1 package org.netxms.ui.android.main.fragments;
2
3 import java.lang.reflect.Method;
4
5 import org.netxms.ui.android.R;
6 import org.netxms.ui.android.main.activities.ConsolePreferences;
7 import org.netxms.ui.android.main.activities.HomeScreen;
8 import org.netxms.ui.android.service.ClientConnectorService;
9
10 import android.app.Activity;
11 import android.content.Intent;
12 import android.os.Bundle;
13 import android.support.v4.app.ListFragment;
14 import android.view.MenuItem;
15 import android.view.View;
16 import android.view.animation.AnimationUtils;
17 import android.widget.ListView;
18 import android.widget.TextView;
19
20 /**
21 * Abstract base class for all fragments in the client.
22 * Implements functionality for passing common parameters and
23 * for handling common tasks.
24 *
25 * @author Marco Incalcaterra (marco.incalcaterra@thinksoft.it)
26 *
27 */
28
29 public abstract class AbstractListFragment extends ListFragment
30 {
31 protected ClientConnectorService service = null;
32 protected long nodeId;
33 private View progressContainer;
34 private View listContainer;
35 private TextView standardEmptyView;
36 protected boolean listShown;
37 protected static Method method_invalidateOptionsMenu;
38
39 static
40 {
41 try
42 {
43 method_invalidateOptionsMenu = Activity.class.getMethod("invalidateOptionsMenu", new Class[0]);
44 }
45 catch (NoSuchMethodException e)
46 {
47 method_invalidateOptionsMenu = null;
48 }
49 }
50
51 @Override
52 public void onCreate(Bundle savedInstanceState)
53 {
54 super.onCreate(savedInstanceState);
55 setRetainInstance(true);
56 setHasOptionsMenu(true);
57 }
58
59 @Override
60 public boolean onContextItemSelected(MenuItem item)
61 {
62 if (defaultMenuAction(item))
63 return true;
64 return super.onContextItemSelected(item);
65 }
66
67 @Override
68 public boolean onOptionsItemSelected(MenuItem item)
69 {
70 if (defaultMenuAction(item))
71 return true;
72 return super.onOptionsItemSelected(item);
73 }
74
75 @Override
76 public void setListShown(boolean shown)
77 {
78 setListShown(shown, true);
79 }
80
81 @Override
82 public void setListShownNoAnimation(boolean shown)
83 {
84 setListShown(shown, false);
85 }
86
87 public void setNodeId(long id)
88 {
89 nodeId = id;
90 }
91
92 public void setService(ClientConnectorService service)
93 {
94 this.service = service;
95 }
96
97 public abstract void refresh();
98
99 public void setListShown(boolean shown, boolean animate)
100 {
101 if (listShown != shown)
102 {
103 listShown = shown;
104 if (shown)
105 {
106 if (animate)
107 {
108 progressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
109 listContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
110 }
111 progressContainer.setVisibility(View.GONE);
112 listContainer.setVisibility(View.VISIBLE);
113 }
114 else
115 {
116 if (animate)
117 {
118 progressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
119 listContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
120 }
121 progressContainer.setVisibility(View.VISIBLE);
122 listContainer.setVisibility(View.INVISIBLE);
123 }
124 }
125 }
126
127 protected void createProgress(View v)
128 {
129 int INTERNAL_EMPTY_ID = 0x00ff0001;
130 listContainer = v.findViewById(R.id.listContainer);
131 progressContainer = v.findViewById(R.id.progressContainer);
132 standardEmptyView = (TextView)v.findViewById(INTERNAL_EMPTY_ID);
133 if (standardEmptyView != null)
134 ((ListView)v).setEmptyView(standardEmptyView);
135
136 listShown = true;
137 }
138
139 private boolean defaultMenuAction(MenuItem item)
140 {
141 switch (item.getItemId())
142 {
143 case android.R.id.home:
144 startActivity(new Intent(getActivity(), HomeScreen.class));
145 return true;
146 case R.id.settings:
147 startActivity(new Intent(getActivity(), ConsolePreferences.class));
148 return true;
149 }
150 return false;
151 }
152 }