summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/AudioplayerActivity.java
blob: 0a4c8ae14574a6433b2abaecbebc08a3598448b8 (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
package de.danoeh.antennapod.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChapterListAdapter;
import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.Chapter;
import de.danoeh.antennapod.feed.MediaType;
import de.danoeh.antennapod.feed.SimpleChapter;
import de.danoeh.antennapod.fragment.CoverFragment;
import de.danoeh.antennapod.fragment.ItemDescriptionFragment;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.util.playback.ExternalMedia;
import de.danoeh.antennapod.util.playback.Playable;

/** Activity for playing audio files. */
public class AudioplayerActivity extends MediaplayerActivity {
	private static final int POS_COVER = 0;
	private static final int POS_DESCR = 1;
	private static final int POS_CHAPTERS = 2;
	private static final int NUM_CONTENT_FRAGMENTS = 3;

	final String TAG = "AudioplayerActivity";
	private static final String PREFS = "AudioPlayerActivityPreferences";
	private static final String PREF_KEY_SELECTED_FRAGMENT_POSITION = "selectedFragmentPosition";
	private static final String PREF_PLAYABLE_ID = "playableId";

	private Fragment[] detachedFragments;

	private CoverFragment coverFragment;
	private ItemDescriptionFragment descriptionFragment;
	private ListFragment chapterFragment;

	private Fragment currentlyShownFragment;
	private int currentlyShownPosition = -1;
	/** Used if onResume was called without loadMediaInfo. */
	private int savedPosition = -1;

	private TextView txtvTitle;
	private TextView txtvFeed;
	private ImageButton butNavLeft;
	private ImageButton butNavRight;

	private void resetFragmentView() {
		FragmentTransaction fT = getSupportFragmentManager().beginTransaction();

		if (coverFragment != null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Removing cover fragment");
			fT.remove(coverFragment);
		}
		if (descriptionFragment != null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Removing description fragment");
			fT.remove(descriptionFragment);
		}
		if (chapterFragment != null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Removing chapter fragment");
			fT.remove(chapterFragment);
		}
		if (currentlyShownFragment != null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Removing currently shown fragment");
			fT.remove(currentlyShownFragment);
		}
		for (int i = 0; i < detachedFragments.length; i++) {
			Fragment f = detachedFragments[i];
			if (f != null) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Removing detached fragment");
				fT.remove(f);
			}
		}
		fT.commit();
		currentlyShownFragment = null;
		coverFragment = null;
		descriptionFragment = null;
		chapterFragment = null;
		currentlyShownPosition = -1;
		detachedFragments = new Fragment[NUM_CONTENT_FRAGMENTS];
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (AppConfig.DEBUG)
			Log.d(TAG, "onStop");

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		detachedFragments = new Fragment[NUM_CONTENT_FRAGMENTS];
	}

	private void savePreferences() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Saving preferences");
		SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		if (currentlyShownPosition >= 0 && controller != null
				&& controller.getMedia() != null) {
			editor.putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION,
					currentlyShownPosition);
			editor.putString(PREF_PLAYABLE_ID, controller.getMedia()
					.getIdentifier().toString());
		} else {
			editor.putInt(PREF_KEY_SELECTED_FRAGMENT_POSITION, -1);
			editor.putString(PREF_PLAYABLE_ID, "");
		}
		editor.commit();

		savedPosition = currentlyShownPosition;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// super.onSaveInstanceState(outState); would cause crash
		if (AppConfig.DEBUG)
			Log.d(TAG, "onSaveInstanceState");
	}

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

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		restoreFromPreferences();
	}

	/**
	 * Tries to restore the selected fragment position from the Activity's
	 * preferences.
	 * 
	 * @return true if restoreFromPrefernces changed the activity's state
	 * */
	private boolean restoreFromPreferences() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Restoring instance state");
		SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
		int savedPosition = prefs.getInt(PREF_KEY_SELECTED_FRAGMENT_POSITION,
				-1);
		String playableId = prefs.getString(PREF_PLAYABLE_ID, "");

		if (savedPosition != -1
				&& controller != null
				&& controller.getMedia() != null
				&& controller.getMedia().getIdentifier().toString()
						.equals(playableId)) {
			switchToFragment(savedPosition);
			return true;
		} else if (controller == null || controller.getMedia() == null) {
			if (AppConfig.DEBUG)
				Log.d(TAG,
						"Couldn't restore from preferences: controller or media was null");
		} else {
			if (AppConfig.DEBUG)
				Log.d(TAG,
						"Couldn't restore from preferences: savedPosition was -1 or saved identifier and playable identifier didn't match.\nsavedPosition: "
								+ savedPosition + ", id: " + playableId);

		}
		return false;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (getIntent().getAction() != null
				&& getIntent().getAction().equals(Intent.ACTION_VIEW)) {
			Intent intent = getIntent();
			if (AppConfig.DEBUG)
				Log.d(TAG, "Received VIEW intent: "
						+ intent.getData().getPath());
			ExternalMedia media = new ExternalMedia(intent.getData().getPath(),
					MediaType.AUDIO);
			Intent launchIntent = new Intent(this, PlaybackService.class);
			launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
			launchIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED,
					true);
			launchIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, false);
			launchIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY,
					true);
			startService(launchIntent);
		}
		if (savedPosition != -1) {
			switchToFragment(savedPosition);
		}
		
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
	}

	@Override
	protected void onAwaitingVideoSurface() {
		startActivity(new Intent(this, VideoplayerActivity.class));
	}

	@Override
	protected void postStatusMsg(int resId) {
		setSupportProgressBarIndeterminateVisibility(resId == R.string.player_preparing_msg
				|| resId == R.string.player_seeking_msg
				|| resId == R.string.player_buffering_msg);
	}

	@Override
	protected void clearStatusMsg() {
		setSupportProgressBarIndeterminateVisibility(false);
	}

	/**
	 * Changes the currently displayed fragment.
	 * 
	 * @param pos Must be POS_COVER, POS_DESCR, or POS_CHAPTERS
	 * */
	private void switchToFragment(int pos) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Switching contentView to position " + pos);
		if (currentlyShownPosition != pos && controller != null) {
			Playable media = controller.getMedia();
			if (media != null) {
				FragmentTransaction ft = getSupportFragmentManager()
						.beginTransaction();
				if (currentlyShownFragment != null) {
					detachedFragments[currentlyShownPosition] = currentlyShownFragment;
					ft.detach(currentlyShownFragment);
				}
				switch (pos) {
				case POS_COVER:
					if (coverFragment == null) {
						Log.i(TAG, "Using new coverfragment");
						coverFragment = CoverFragment.newInstance(media);
					}
					currentlyShownFragment = coverFragment;
					break;
				case POS_DESCR:
					if (descriptionFragment == null) {
						descriptionFragment = ItemDescriptionFragment
								.newInstance(media, true);
					}
					currentlyShownFragment = descriptionFragment;
					break;
				case POS_CHAPTERS:
					if (chapterFragment == null) {
						chapterFragment = new ListFragment() {

							@Override
							public void onListItemClick(ListView l, View v,
									int position, long id) {
								super.onListItemClick(l, v, position, id);
								Chapter chapter = (Chapter) this
										.getListAdapter().getItem(position);
								controller.seekToChapter(chapter);
							}

						};
						chapterFragment.setListAdapter(new ChapterListAdapter(
								AudioplayerActivity.this, 0, media
										.getChapters(), media));
					}
					currentlyShownFragment = chapterFragment;
					break;
				}
				if (currentlyShownFragment != null) {
					currentlyShownPosition = pos;
					if (detachedFragments[pos] != null) {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Reattaching fragment at position "
									+ pos);
						ft.attach(detachedFragments[pos]);
					} else {
						ft.add(R.id.contentView, currentlyShownFragment);
					}
					ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
					ft.disallowAddToBackStack();
					ft.commit();
					updateNavButtonDrawable();
				}
			}
		}
	}

	private void updateNavButtonDrawable() {
		TypedArray drawables = obtainStyledAttributes(new int[] {
				R.attr.navigation_shownotes, R.attr.navigation_chapters });
		final Playable media = controller.getMedia();
		if (butNavLeft != null && butNavRight != null && media != null) {
			switch (currentlyShownPosition) {
			case POS_COVER:
				butNavLeft.setScaleType(ScaleType.CENTER);
				butNavLeft.setImageDrawable(drawables.getDrawable(0));
				butNavRight.setImageDrawable(drawables.getDrawable(1));
				break;
			case POS_DESCR:
				butNavLeft.setScaleType(ScaleType.CENTER_CROP);
				butNavLeft.post(new Runnable() {

					@Override
					public void run() {
						ImageLoader.getInstance().loadThumbnailBitmap(media,
								butNavLeft);
					}
				});
				butNavRight.setImageDrawable(drawables.getDrawable(1));
				break;
			case POS_CHAPTERS:
				butNavLeft.setScaleType(ScaleType.CENTER_CROP);
				butNavLeft.post(new Runnable() {

					@Override
					public void run() {
						ImageLoader.getInstance().loadThumbnailBitmap(media,
								butNavLeft);
					}
				});
				butNavRight.setImageDrawable(drawables.getDrawable(0));
				break;
			}
		}
	}

	@Override
	protected void setupGUI() {
		super.setupGUI();
		resetFragmentView();
		txtvTitle = (TextView) findViewById(R.id.txtvTitle);
		txtvFeed = (TextView) findViewById(R.id.txtvFeed);
		butNavLeft = (ImageButton) findViewById(R.id.butNavLeft);
		butNavRight = (ImageButton) findViewById(R.id.butNavRight);

		butNavLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (currentlyShownFragment == null
						|| currentlyShownPosition == POS_DESCR) {
					switchToFragment(POS_COVER);
				} else if (currentlyShownPosition == POS_COVER) {
					switchToFragment(POS_DESCR);
				} else if (currentlyShownPosition == POS_CHAPTERS) {
					switchToFragment(POS_COVER);
				}
			}
		});

		butNavRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (currentlyShownPosition == POS_CHAPTERS) {
					switchToFragment(POS_DESCR);
				} else {
					switchToFragment(POS_CHAPTERS);
				}
			}
		});
	}

	@Override
	protected void onPositionObserverUpdate() {
		super.onPositionObserverUpdate();
		notifyMediaPositionChanged();
	}

	@Override
	protected void loadMediaInfo() {
		super.loadMediaInfo();
		final Playable media = controller.getMedia();
		if (media != null) {
			txtvTitle.setText(media.getEpisodeTitle());
			txtvFeed.setText(media.getFeedTitle());
			if (media.getChapters() != null) {
				butNavRight.setVisibility(View.VISIBLE);
			} else {
				butNavRight.setVisibility(View.GONE);
			}

		}
		if (currentlyShownPosition == -1) {
			if (!restoreFromPreferences()) {
				switchToFragment(POS_COVER);
			}
		}
		if (currentlyShownFragment instanceof AudioplayerContentFragment) {
			((AudioplayerContentFragment) currentlyShownFragment)
					.onDataSetChanged(media);
		}

	}

	public void notifyMediaPositionChanged() {
		if (chapterFragment != null) {
			ArrayAdapter<SimpleChapter> adapter = (ArrayAdapter<SimpleChapter>) chapterFragment
					.getListAdapter();
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onReloadNotification(int notificationCode) {
		if (notificationCode == PlaybackService.EXTRA_CODE_VIDEO) {
			if (AppConfig.DEBUG)
				Log.d(TAG,
						"ReloadNotification received, switching to Videoplayer now");
			startActivity(new Intent(this, VideoplayerActivity.class));

		}
	}

	@Override
	protected void onBufferStart() {
		postStatusMsg(R.string.player_buffering_msg);
	}

	@Override
	protected void onBufferEnd() {
		clearStatusMsg();
	}

	public interface AudioplayerContentFragment {
		public void onDataSetChanged(Playable media);
	}

	@Override
	protected int getContentViewResourceId() {
		return R.layout.audioplayer_activity;
	}

}