summaryrefslogtreecommitdiff
path: root/vdrmanager/src/de/bjusystems/vdrmanager/gui/VdrManagerActivity.java
blob: 7543ad7993d3ca0baa2f6b970b53d20fdd5910dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package de.bjusystems.vdrmanager.gui;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import de.bjusystems.vdrmanager.R;
import de.bjusystems.vdrmanager.app.VdrManagerApp;
import de.bjusystems.vdrmanager.data.MenuActionHandler;
import de.bjusystems.vdrmanager.data.Preferences;
import de.bjusystems.vdrmanager.utils.wakeup.AsyncWakeupTask;

public class VdrManagerActivity extends Activity {

	static class WakeupAction implements MenuActionHandler {
		public void executeAction(final Context context) {
			// create AsyncTask
			final AsyncWakeupTask wakeupTask = new AsyncWakeupTask(context);
			wakeupTask.execute();
		}
	}

	@SuppressWarnings("rawtypes")
	static class MenuActivity {
		int iconId;
		int textId;
		Class handler;
		boolean enabled;

		public int getIconId() {
			return iconId;
		}
		public int getTextId() {
			return textId;
		}

		public Class getHandler() {
			return handler;
		}

		public boolean isEnabled() {
			return enabled;
		}

		public MenuActivity(final int iconId, final int textId, final Class handler) {
			this.iconId = iconId;
			this.textId = textId;
			this.handler = handler;
			this.enabled = true;
		}

		public void setEnabled(final boolean enabled) {
			this.enabled = enabled;
		}
	}

	class MenuAdapter extends ArrayAdapter<MenuActivity> implements OnClickListener {

		private final LayoutInflater inflater;

		public MenuAdapter(final Context context, final int viewId) {
			super(context, viewId);
			inflater = LayoutInflater.from(context);
		}

		@SuppressWarnings("unchecked")
		@Override
		public View getView(final int position, final View convertView, final ViewGroup parent) {

			View view = convertView;
			View itemView;
			if (view == null) {
				view = inflater.inflate(R.layout.vdrmanager_menu_item, null);
				itemView = view.findViewById(R.id.vdrmanager_menu_item);
				view.setTag(itemView);

				// attach click listener
				itemView.setOnClickListener(this);

			} else {
				itemView = (View) view.getTag();
			}

			// attach menu item
			final MenuActivity menuItem = getItem(position);
			if (itemView instanceof TextView) {
				final TextView textView = (TextView) itemView;
				textView.setText(menuItem.getTextId());
			} else {
				final ImageButton imageButton = (ImageButton) itemView;
				imageButton.setImageResource(menuItem.getIconId());
			}
			itemView.setOnClickListener(this);
			menuActivityMap.put(itemView, menuItem.getHandler());

			return view;
		}

		@SuppressWarnings({ "unchecked", "rawtypes" })
		public void onClick(final View v) {

			// refresh preferences
			Preferences.loadPreferences(getContext());

			// clear state
			((VdrManagerApp) getApplication()).clear();

			// start activity
			final Class actionClass = menuActivityMap.get(v);
			if (Activity.class.isAssignableFrom(actionClass)) {
				final Intent intent = new Intent();
				intent.setClass(getContext(), actionClass);
				startActivity(intent);
			} else if (MenuActionHandler.class.isAssignableFrom(actionClass)) {
				final Class<? extends MenuActionHandler> handlerClass = actionClass;
				try {
					final MenuActionHandler handler = handlerClass.newInstance();
					handler.executeAction(getContext());
				} catch (final InstantiationException e) {

				} catch (final IllegalAccessException e) {

				}
			}
		}
	}

	MenuActivity[] menuItems = new MenuActivity[] {
		new MenuActivity(R.drawable.whatson, R.string.action_menu_epg, EpgListActivity.class),
		new MenuActivity(0, R.string.action_menu_search, EpgSearchActivity.class),
		new MenuActivity(R.drawable.timers, R.string.action_menu_timers, TimerListActivity.class),
		new MenuActivity(R.drawable.channels, R.string.action_menu_channels, ChannelListActivity.class),
		new MenuActivity(0, R.string.action_menu_wakeup, WakeupAction.class),
	};
	Map<View, Class<? extends Activity>> menuActivityMap = new HashMap<View, Class<? extends Activity>>();

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// attach view
		setContentView(R.layout.vdrmanager);

		// add and register buttons
		createButtons();
	}

	// add main menu buttons
	private void createButtons() {

		// refresh preferences
		Preferences.loadPreferences(this);
		final Preferences prefs = Preferences.getPreferences();

		// get list
		final AbsListView listView = (AbsListView) findViewById(R.id.vdrmanager_menu);
		final MenuAdapter adapter =	new MenuAdapter(this, R.layout.vdrmanager_menu_item);
		listView.setAdapter(adapter);

		// add menu items
		for(final MenuActivity menuItem : menuItems) {
			if (menuItem.getTextId() != R.string.action_menu_wakeup || prefs.isWakeupEnabled()) {
				adapter.add(menuItem);
			}
		}

		// set grid layout dimensions
		if (listView instanceof GridView) {
			final GridView grid = (GridView)listView;
			if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				grid.setNumColumns(2);
			} else {
				grid.setNumColumns(menuItems.length);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);

    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {

		switch (item.getItemId())
		{
		case R.id.main_menu_preferences:
			// remember activity for finishing
			final VdrManagerApp app = (VdrManagerApp) getApplication();
			app.clearActivitiesToFinish();
			app.addActivityToFinish(this);

			final Intent intent = new Intent();
			intent.setClass(this, PreferencesActivity.class);
			startActivity(intent);
			break;
		case R.id.main_menu_exit:
			finish();
			break;
		}
		return true;
	}

	@Override
	public void onBackPressed() {
	}
}