summaryrefslogtreecommitdiff
path: root/vdrmanager/src/de/bjusystems/vdrmanager/gui/EpgDetailsActivity.java
blob: 1eadf345aa2c396236440a1f789127f2545df0d7 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package de.bjusystems.vdrmanager.gui;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.viewpagerindicator.TitleProvider;

import de.bjusystems.vdrmanager.R;
import de.bjusystems.vdrmanager.app.Intents;
import de.bjusystems.vdrmanager.app.VdrManagerApp;
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.Recording;
import de.bjusystems.vdrmanager.data.Timer;
import de.bjusystems.vdrmanager.data.TimerMatch;
import de.bjusystems.vdrmanager.data.Timerable;
import de.bjusystems.vdrmanager.data.Timerable.TimerState;
import de.bjusystems.vdrmanager.tasks.DeleteTimerTask;
import de.bjusystems.vdrmanager.tasks.ToggleTimerTask;
import de.bjusystems.vdrmanager.tasks.VoidAsyncTask;
import de.bjusystems.vdrmanager.utils.svdrp.SvdrpEvent;

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

	public static final String TAG = "EpgDetailsActivity";

	public static String IMDB_BASE_URL = "http://%s";

	public static String IMDB_URL_QUERY = "/find?s=tt&q=%s";

	public static String OMDB_URL = "http://www.omdb.org/search?search[text]=%s";

	private static final String IMDB_URL_ENCODING = "UTF-8";

	private static final String OMDB_URL_ENCODING = "UTF-8";

	private static final String TMDB_URL_ENCODING = "UTF-8";

	public static String TMDB_URL = "http://www.themoviedb.org/search?search=%s";

	private String highlight = null;

	private Event cEvent;

	// private ImageView state;

	private boolean modifed = false;

	private int current;

	private ViewPager pager;

	private Timerable timerable = null;

	class Adapter extends PagerAdapter implements TitleProvider {

		public Adapter() {

		}

		public String getTitle(int position) {
			return epgs.get(position).getChannelName();
		}

		public int getCount() {
			return epgs.size();
		}

		public Object instantiateItem(View pager, int position) {
			View view = getLayoutInflater().inflate(R.layout.epg_detail, null);
			// Event e = epgs.get(position);
			publishEPG(view, position);
			((ViewPager) pager).addView(view, 0);

			return view;
		}

		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((View) view);
		}

		public boolean isViewFromObject(View view, Object object) {
			return view.equals(object);
		}

		public void finishUpdate(View view) {
		}

		public void restoreState(Parcelable p, ClassLoader c) {
		}

		public Parcelable saveState() {
			return null;
		}

		public void startUpdate(View view) {
		}
	}

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

		// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		Intent i = getIntent();

		highlight = i.getStringExtra(Intents.HIGHLIGHT);

		initActionBar();

		// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
		// R.layout.titlebar);

		// Attach view
		setContentView(R.layout.epgdetails);

		// detector = new SimpleGestureFilter(this, this);

		// state = (ImageView) findViewById(R.id.epg_timer_state);

		final Event epg = getApp().getCurrentEvent();
		if (epg == null) {
			finish();
		}

		cEvent = epg;

		if (epg instanceof Timerable) {
			timerable = (Timerable) epg;
		}

		new VoidAsyncTask() {

			int counter = 0;

			@Override
			protected void onPreExecute() {
				setProgressBarIndeterminateVisibility(true);
			}

			@Override
			protected Void doInBackground(Void... params) {
				// current event
				final VdrManagerApp app = (VdrManagerApp) getApplication();
				epgs = app.getCurrentEpgList();

				if (epgs.isEmpty()) {
					epgs.add(cEvent);
					return (Void) null;
				}

				for (Event e : epgs) {
					if (epg.equals(e)) {
						break;
					}
					counter++;
				}

				if (counter == epgs.size()) {// not found?
					epgs.add(0, cEvent);
					counter = 0;
				}
				return (Void) null;
			}

			@Override
			protected void onPostExecute(Void result) {

				Adapter adapter = new Adapter();
				ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
				EpgDetailsActivity.this.pager = pager;
				pager.setOnPageChangeListener(EpgDetailsActivity.this);
				// TitlePageIndicator indicator = (TitlePageIndicator)
				// findViewById(R.id.indicator);
				pager.setAdapter(adapter);
				cEvent = epgs.get(counter);
				pager.setCurrentItem(counter);
				current = counter;
				// indicator.setViewPager(pager);

				// publishEPG(epg);
			}
		}.execute((Void) null);

	}

	private void setState(ImageView view, int res) {
		view.setVisibility(View.VISIBLE);
		view.setImageResource(res);
	}

	private static String encode(String str, String enc) {
		try {
			return URLEncoder.encode(str, enc);
		} catch (Exception ex) {
			Log.w(TAG, ex);
			return URLEncoder.encode(str);
		}
	}

	public void publishEPG(final View view, int position) {

		Event event = epgs.get(position);

		// view.setTag(event);

		final EventFormatter formatter = new EventFormatter(event);

		final TextView title = (TextView) view
				.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) view.findViewById(R.id.epg_detail_time)).setText(formatter
				.getDate() + " " + formatter.getTime());

		TextView dura = (TextView) view.findViewById(R.id.epg_detail_duration);

		((TextView) view.findViewById(R.id.epg_detail_channel)).setText(event
				.getChannelName());
		// ((TextView) findViewById(R.id.epg_detail_date)).setText(formatter
		// .getLongDate());
		ImageView state = (ImageView) view.findViewById(R.id.epg_timer_state);
		if (timerable == null) {
			setState(state, R.drawable.timer_none);
		} else {

			TimerMatch match = timerable.getTimerMatch();

			switch (timerable.getTimerState()) {
			case Active:
				setState(state, Utils.getTimerStateDrawable(match, R.drawable.timer_active, R.drawable.timer_active_begin, R.drawable.timer_active_end) );
				break;
			case Inactive:
				setState(state, Utils.getTimerStateDrawable(match, R.drawable.timer_inactive,R.drawable.timer_inactive_begin, R.drawable.timer_inactive_end));
				break;
			case Recording:
				setState(state, Utils.getTimerStateDrawable(match, R.drawable.timer_recording, R.drawable.timer_recording_begin, R.drawable.timer_recording_end));
				break;
			default:
				setState(state, R.drawable.timer_none);
			}
		}
		final TextView shortText = (TextView) view
				.findViewById(R.id.epg_detail_shorttext);
		shortText.setText(Utils.highlight(formatter.getShortText(), highlight));

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



		if(cEvent.getAudio().isEmpty() == false){
			final TextView audioTracks = (TextView) view.findViewById(R.id.epg_detail_audio);
			audioTracks.setText(Utils.formatAudio(this, cEvent.getAudio()));
		} else {
			view.findViewById(R.id.audio_image).setVisibility(View.GONE);
		}




		// 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) view.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) view.findViewById(R.id.epg_detail_separator_2))
		// .setBackgroundColor(color);

		// register button handler
		if (timerable == null) {
			view.findViewById(R.id.epg_event_create_timer).setVisibility(
					View.GONE);
		} else {
			setThisAsOnClickListener(view, R.id.epg_event_create_timer);
		}

		View b = view.findViewById(R.id.epg_event_imdb);

		if (Preferences.get().isShowImdbButton() == false) {
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			b.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					startFilmDatabaseBrowseIntent(
							String.format(IMDB_BASE_URL, Preferences.get()
									.getImdbUrl())
									+ IMDB_URL_QUERY, view, IMDB_URL_ENCODING);
				}
			});
		}

		b = view.findViewById(R.id.epg_event_omdb);

		if (Preferences.get().isShowOmdbButton() == false) {
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			b.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					startFilmDatabaseBrowseIntent(OMDB_URL, view,
							OMDB_URL_ENCODING);
				}
			});
		}

		b = view.findViewById(R.id.epg_event_tmdb);

		if (Preferences.get().isShowTmdbButton() == false) {
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			b.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					startFilmDatabaseBrowseIntent(TMDB_URL, view,
							TMDB_URL_ENCODING);
				}
			});
		}

		b = view.findViewById(R.id.epg_event_livetv);
		if (Utils.isLive(event) == false
				&& (event instanceof Recording == false || Preferences.get()
						.isEnableRecStream() == false)) {
			b.setVisibility(View.GONE);
		} else {
			b.setVisibility(View.VISIBLE);
			setThisAsOnClickListener(b);
		}
		// setThisAsOnClickListener(view, R.id.epg_event_left);
		// setThisAsOnClickListener(view, 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 startFilmDatabaseBrowseIntent(String url, View view,
			String encoding) {
		final TextView title = (TextView) view
				.findViewById(R.id.epg_detail_title);
		url = String.format(url,
				encode(String.valueOf(title.getText()), encoding));
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		i.addCategory(Intent.CATEGORY_BROWSABLE);
		try {
			startActivity(i);
		} catch (ActivityNotFoundException anfe) {
			Log.w(TAG, anfe);
			say(anfe.getLocalizedMessage());
		}
	}

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


	private void setThisAsOnClickListener(View root, int view) {
		setThisAsOnClickListener(root.findViewById(view));
	}

	@Override
	protected void onResume() {
		super.onResume();
		// TODO Check here whether the config has changed for imdb
	}

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

	class Wrapper {
		public int id;
		public String value;

		public Wrapper(int id) {
			this.id = id;
			this.value = getString(id);
		}

		public String toString() {
			return value;
		}
	}

	public Timer getTimer(Event event) {
		if (event instanceof Timer) {
			return (Timer) event;
		}
		if (event instanceof Epg) {
			return ((Epg) event).getTimer();
		}
		return null;
	}

	protected VdrManagerApp getApp() {
		final VdrManagerApp app = (VdrManagerApp) getApplication();
		return app;
	}

	public void onClick(final View v) {
		switch (v.getId()) {
		case R.id.epg_event_livetv:
			if (cEvent instanceof Recording) {
				Utils.streamRecording(this, (Recording) cEvent);
			} else {
				Utils.stream(this, String.valueOf(cEvent.getChannelNumber()));
			}
			break;
		case R.id.epg_event_create_timer:
			final ArrayAdapter<Wrapper> ada = new ArrayAdapter<Wrapper>(this,
					android.R.layout.simple_dropdown_item_1line);
			final Timer timer = getTimer(cEvent);
			// remove unneeded menu items
			if (timer != null) {
				ada.add(new Wrapper(R.string.epg_item_menu_timer_modify));
				ada.add(new Wrapper(R.string.epg_item_menu_timer_delete));
				if (timer.isEnabled()) {
					ada.add(new Wrapper(R.string.epg_item_menu_timer_disable));
				} else {
					ada.add(new Wrapper(R.string.epg_item_menu_timer_enable));
				}
			} else if (cEvent instanceof Recording) {
				ada.add(new Wrapper(R.string.epg_item_menu_timer_delete));

			} else {
				ada.add(new Wrapper(R.string.epg_item_menu_timer_add));
			}
			new AlertDialog.Builder(this)
					.setAdapter(ada, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							final Timer t;
							if (timer == null) {
								t = timerable.createTimer();
							} else {
								t = timer;
							}
							getApp().setCurrentTimer(t);
							Wrapper w = ada.getItem(which);
							switch (w.id) {
							case R.string.epg_item_menu_timer_add: {
								final Intent intent = new Intent();
								intent.setClass(EpgDetailsActivity.this,
										TimerDetailsActivity.class);
								intent.putExtra(Intents.TIMER_OP,
										Intents.ADD_TIMER);
								startActivityForResult(
										intent,
										TimerDetailsActivity.REQUEST_CODE_TIMER_ADD);
								break;
							}
							case R.string.epg_item_menu_timer_modify: {
								final Intent intent = new Intent();
								intent.setClass(EpgDetailsActivity.this,
										TimerDetailsActivity.class);
								intent.putExtra(Intents.TIMER_OP,
										Intents.EDIT_TIMER);
								startActivityForResult(
										intent,
										TimerDetailsActivity.REQUEST_CODE_TIMER_EDIT);
								break;
							}
							case R.string.epg_item_menu_timer_delete: {
								deleteTimer(timer);
								break;
							}
							case R.string.epg_item_menu_timer_enable:
							case R.string.epg_item_menu_timer_disable: {
								toggleTimer(timer);
								break;
							}
							}
						}
					}).create()//
					.show();//

			break;
		// case R.id.epg_event_imdb:

		// break;

		// case R.id.epg_event_share:
		// shareEvent(cEvent);
		// break;
		}
	}

	protected void toggleTimer(final Timer timer) {
		final ToggleTimerTask task = new ToggleTimerTask(this, timer) {
			@Override
			public void finished(SvdrpEvent event) {
				if (event == SvdrpEvent.FINISHED_SUCCESS) {
					TimerState state = timer.getTimerState();
					TimerMatch match = timer.getTimerMatch();
					int res = -1;
					if (state == TimerState.Active) {
						res = Utils.getTimerStateDrawable(match, R.drawable.timer_inactive, R.drawable.timer_inactive_begin, R.drawable.timer_inactive_end);
					} else if (state == TimerState.Inactive) {
						Utils.getTimerStateDrawable(match,R.drawable.timer_active, R.drawable.timer_active_begin, R.drawable.timer_active_end);
						res = R.drawable.timer_active;
					}
					if (res != -1) {
						setState(
								(ImageView) findViewById(R.id.epg_timer_state),
								res);
					}
				}
			}
		};
		task.start();
	}

	private List<Event> epgs = new ArrayList<Event>();

	protected void say(int res) {
		Toast.makeText(this, res, Toast.LENGTH_SHORT).show();
	}

	protected void say(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

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

		final com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.epg_details, menu);

		// mShareActionProvider = (ShareActionProvider)
		// menu.findItem(R.id.epg_details_menu_share).getActionProvider();
		// mShareActionProvider.setShareIntent(getDefaultShareIntent());

		return true;
	}

	private void shareEvent(Event event) {
		Utils.shareEvent(this, event);
	}

	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		if (item.getItemId() == R.id.epg_details_menu_share) {
			shareEvent(cEvent);
			return true;
		}

		if (item.getItemId() == R.id.epg_details_menu_add_to_cal) {
			Utils.addCalendarEvent(this, cEvent);
		}

		if (item.getItemId() == R.id.epg_details_menu_search_repeat) {
			Intent intent = new Intent(this, EpgSearchListActivity.class);
			intent.setAction(Intent.ACTION_SEARCH);
			intent.putExtra(SearchManager.QUERY, cEvent.getTitle());
			startActivity(intent);
			return true;
		}

		if (item.getItemId() == R.id.epg_details_menu_switch) {
			Utils.switchTo(this, cEvent.getChannelId(), cEvent.getChannelName());
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	protected void deleteTimer(final Timer timer) {
		final DeleteTimerTask task = new DeleteTimerTask(this, timer) {
			@Override
			public void finished(SvdrpEvent event) {
				if (event == SvdrpEvent.FINISHED_SUCCESS) {
					setState((ImageView) findViewById(R.id.epg_timer_state),
							R.drawable.timer_none);
					modifed = true;
				}
			}
		};
		task.start();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}

		pager.getAdapter().notifyDataSetChanged();
		// View view = pager.getChildAt(current);
		// ImageView state = (ImageView)
		// view.findViewById(R.id.epg_timer_state);

		if (requestCode == TimerDetailsActivity.REQUEST_CODE_TIMER_ADD) {
			modifed = true;
			// setState(
			// state,
			// Utils.isLive(getApp().getCurrentEvent()) ?
			// R.drawable.timer_recording
			// : R.drawable.timer_active);
		} else if (requestCode == TimerDetailsActivity.REQUEST_CODE_TIMER_EDIT) {
			modifed = true;

			// ??
		}
	}

	@Override
	public void onBackPressed() {
		if (modifed) {
			setResult(RESULT_OK);
			finish();
		} else {
			super.onBackPressed();
		}
	}

	public void onPageScrollStateChanged(int arg0) {
	}

	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	public void onPageSelected(int arg0) {
		current = arg0;
		cEvent = epgs.get(current);
		String cn = cEvent.getChannelName();
		// View view = pager.getChildAt(arg0);
		// state = (ImageView) view.findViewById(R.id.epg_timer_state);
		setTitle(getString(R.string.epg_of_a_channel, cn, current + 1,
				epgs.size()));
	}

}