summaryrefslogtreecommitdiff
path: root/vdrmanager/src/de/bjusystems/vdrmanager/gui/EpgDetailsActivity.java
blob: b0a7c015862750dff28d4fef1091ecea68b90bfe (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package de.bjusystems.vdrmanager.gui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import de.bjusystems.vdrmanager.R;
import de.bjusystems.vdrmanager.app.Intents;
import de.bjusystems.vdrmanager.app.VdrManagerApp;
import de.bjusystems.vdrmanager.data.Channel;
import de.bjusystems.vdrmanager.data.Epg;
import de.bjusystems.vdrmanager.data.Event;
import de.bjusystems.vdrmanager.data.EventFormatter;
import de.bjusystems.vdrmanager.data.Preferences;
import de.bjusystems.vdrmanager.data.Timer;
import de.bjusystems.vdrmanager.gui.SimpleGestureFilter.SimpleGestureListener;
import de.bjusystems.vdrmanager.utils.svdrp.EpgClient;
import de.bjusystems.vdrmanager.utils.svdrp.SvdrpException;

/**
 * This class is used for showing what's current running on all channels
 * 
 * @author bju
 */
public class EpgDetailsActivity extends Activity implements OnClickListener,
		SimpleGestureListener {

	public static String IMDB_URL = "http://www.%s/find?s=all&q=%s";
	
	private SimpleGestureFilter detector;
	
	private String highlight = null;
	
	ImageButton event_left;
	
	ImageButton event_right;
	
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent  i = getIntent();
		highlight =  i.getStringExtra(Intents.HIGHLIGHT);
		
		// Attach view
		setContentView(R.layout.epg_detail);
		
		
		detector = new SimpleGestureFilter(this, this);

		//event_left = (ImageButton) findViewById(R.id.epg_event_left);
		//event_right = (ImageButton) findViewById(R.id.epg_event_right);
		
		
		// current event
		final VdrManagerApp app = (VdrManagerApp) getApplication();
		epgs = app.getCurrentEpgList();
		Event epg = app.getCurrentEvent();
	
		
		counter = 0;
		for(Event e : epgs){
			if(epg == e){
				break;
			}
			counter++;
		}
				

		
		new AsyncTask<Void,Void,Void>(){
			
			protected void onPreExecute() {
//				event_left.setEnabled(false);
	//			event_right.setEnabled(false);
			};
			
			protected  Void doInBackground(Void... params) {
				initEPGs();
				return null;
			};
			
			protected void onPostExecute(Void result) {
		//		event_left.setEnabled(true);
			//	event_right.setEnabled(true);
			};
			
		}.execute((Void)null);

		publishEPG(epg);
		
		//TODO was ist das?
		app.clearActivitiesToFinish();
	}

	public void publishEPG(Event event) {
		
		String cn = event.getChannelName();
		
		setTitle(getString(R.string.epg_of_a_channel,cn));
		
		final EventFormatter formatter = new EventFormatter(event);

		final TextView title = (TextView) findViewById(R.id.epg_detail_title);
		String titleText = formatter.getTitle();
		title.setText(Utils.highlight(titleText, highlight));
		// title.setTextSize(TypedValue.COMPLEX_UNIT_PX, title.getTextSize()
		// * (float) 1.3);

		((TextView) findViewById(R.id.epg_detail_time)).setText(formatter
				.getDate() + " " + formatter.getTime());
		
		TextView dura = (TextView) findViewById(R.id.epg_detail_duration);
		
		((TextView) findViewById(R.id.epg_detail_channel)).setText(event
				.getChannelName());
		//((TextView) findViewById(R.id.epg_detail_date)).setText(formatter
			//	.getLongDate());
		
		final TextView shortText = (TextView) findViewById(R.id.epg_detail_shorttext);
		shortText.setText(Utils.highlight(formatter.getShortText(), highlight));

		
		final TextView textView = (TextView) findViewById(R.id.epg_detail_description);
		textView.setText(Utils.highlight(formatter.getDescription(), highlight));

		// copy color for separator lines
		final int color = textView.getTextColors().getDefaultColor();
		// ((TextView) findViewById(R.id.epg_detail_separator_1))
		// .setBackgroundColor(color);

		int p = Utils.getProgress(event);
		
		((ProgressBar) findViewById(R.id.epg_detail_progress))
				.setProgress(p);
		int dm = Utils.getDuration(event);
		if(Utils.isLive(event)){
			int rest = dm - (dm * p / 100);
			dura.setText(getString(R.string.epg_duration_template_live, rest, dm));
		} else {
			dura.setText(getString(R.string.epg_duration_template, dm));
		}

		((TextView) findViewById(R.id.epg_detail_separator_2))
				.setBackgroundColor(color);

		// register button handler
		setThisAsOnClickListener(R.id.epg_event_create_timer);
		
		
		View b = findViewById(R.id.epg_event_imdb);
	
		if(Preferences.get().isShowImdbButton() == false){
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			setThisAsOnClickListener(b);
		}
		
		b = findViewById(R.id.epg_event_livetv);
		if (Utils.isLive(event) == false) {
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			setThisAsOnClickListener(b);
		}
//		setThisAsOnClickListener(R.id.epg_event_left);
//		setThisAsOnClickListener(R.id.epg_event_right);

		// set button text
		if (event instanceof Timer) {
			// timeButton.setText(R.string.epg_event_create_timer_text);
		} else {
			// timeButton.setText(R.string.epg_event_modify_timer_text);
		}

	}

	private void setThisAsOnClickListener(View v) {
		if (v != null) {
			v.setOnClickListener(this);
		}
	}

	private void setThisAsOnClickListener(int view) {
		setThisAsOnClickListener(findViewById(view));
	}

	@Override
	protected void onResume() {
		super.onResume();
		//TODO Check here whether the config has changed for imdb
		//TODO check here if we are still live ?
		
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public void onClick(final View v) {

		final VdrManagerApp app = (VdrManagerApp) getApplication();
		switch (v.getId()) {
		case R.id.epg_event_livetv:
			Event event = app.getCurrentEvent();
			Utils.stream(this, event.getChannelNumber());
			break;
		case R.id.epg_event_create_timer:
			Toast.makeText(this, "Soon we get here the timer menu", Toast.LENGTH_SHORT).show();
			break;
		case R.id.epg_event_imdb:
			final TextView title = (TextView) findViewById(R.id.epg_detail_title);
			String url = String.format(IMDB_URL,Preferences.get().getImdbUrl(), String.valueOf(title.getText()));
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(url));
			startActivity(i);
			break;
		}
	}

	public void onSwipe(int direction) {
		switch (direction) {
		case SimpleGestureFilter.SWIPE_RIGHT:
			prevEPG();
			break;
		case SimpleGestureFilter.SWIPE_LEFT:
			nextEPG();
			break;
		}
	}

	private void prevEPG() {
		Event epg;
		if (counter == 0) {
			epg = epgs.get(0);
		} else {
			epg = epgs.get(--counter);
		}

		publishEPG(epg);

	}

	List<Event> epgs = null;
	int counter = 0;

	public void initEPGs() {
		
		if (epgs != null) {
			return;
		}
		epgs = new ArrayList<Event>();

		final VdrManagerApp app = (VdrManagerApp) getApplication();
		final Event event = app.getCurrentEvent();
		EpgClient c = new EpgClient(new Channel() {
			@Override
			public String getName() {
				return event.getChannelName();
			}

			@Override
			public int getNumber() {
				return Integer.valueOf(event.getChannelNumber());
			}
		});

		try {
			c.run();
		} catch (SvdrpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Epg> e = c.getResults();
		if (e == null || e.isEmpty()) {
			return;
		}

		epgs.addAll(e);
		Event fe = epgs.get(0);
		if (event.getStart().equals(fe.getStart()) == false) {
			epgs.set(0, event);
			;
		}
	}

	private void nextEPG() {
		if(counter < epgs.size() - 1){
			counter ++ ;
		}
		Event epg = epgs.get(counter);
		publishEPG(epg);
	}

	public void onDoubleTap() {

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent me) {
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

}