diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-07-16 12:13:20 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-07-16 12:13:20 +0200 |
commit | 8236f49f90a6b968002bb428247bd555a7a305be (patch) | |
tree | 261549a0075738dee31f82c8f39ef4c685e7b606 /src | |
parent | ea67e94edb995f9a4fb19e9e55cd8e4b7a8c98d5 (diff) | |
download | AntennaPod-8236f49f90a6b968002bb428247bd555a7a305be.zip |
Code rearrangements + bugfixes
Diffstat (limited to 'src')
-rw-r--r-- | src/de/danoeh/antennapod/activity/MediaplayerActivity.java | 237 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/service/PlaybackService.java | 2 |
2 files changed, 131 insertions, 108 deletions
diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java index 2cc4406e9..6ef67714f 100644 --- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -19,6 +19,7 @@ import android.os.IBinder; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.MotionEvent; @@ -127,10 +128,13 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements @Override public boolean onPrepareOptionsMenu(Menu menu) { - - menu.findItem(R.id.support_item).setVisible(media != null && media.getItem().getPaymentLink() != null); - menu.findItem(R.id.share_link_item).setVisible(media != null && media.getItem().getLink() != null); - menu.findItem(R.id.visit_website_item).setVisible(media != null && media.getItem().getLink() != null); + + menu.findItem(R.id.support_item).setVisible( + media != null && media.getItem().getPaymentLink() != null); + menu.findItem(R.id.share_link_item).setVisible( + media != null && media.getItem().getLink() != null); + menu.findItem(R.id.visit_website_item).setVisible( + media != null && media.getItem().getLink() != null); return true; } @@ -141,7 +145,8 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements finish(); break; default: - return FeedItemMenuHandler.onMenuItemClicked(this, item, media.getItem()); + return FeedItemMenuHandler.onMenuItemClicked(this, item, + media.getItem()); } return true; } @@ -312,12 +317,11 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements if (!mediaInfoLoaded) { Log.d(TAG, "Loading media info"); if (media != null) { - + getSupportActionBar().setSubtitle( + media.getItem().getTitle()); + getSupportActionBar().setTitle( + media.getItem().getFeed().getTitle()); if (orientation == Configuration.ORIENTATION_PORTRAIT) { - getSupportActionBar().setSubtitle( - media.getItem().getTitle()); - getSupportActionBar().setTitle( - media.getItem().getFeed().getTitle()); pagerAdapter.notifyDataSetChanged(); } @@ -496,16 +500,37 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements public void onServiceConnected(ComponentName className, IBinder service) { playbackService = ((PlaybackService.LocalBinder) service) .getService(); - int requestedOrientation; - status = playbackService.getStatus(); - media = playbackService.getMedia(); - invalidateOptionsMenu(); + registerReceiver(statusUpdate, new IntentFilter( PlaybackService.ACTION_PLAYER_STATUS_CHANGED)); registerReceiver(notificationReceiver, new IntentFilter( PlaybackService.ACTION_PLAYER_NOTIFICATION)); + queryService(); + Log.d(TAG, "Connection to Service established"); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + playbackService = null; + Log.d(TAG, "Disconnected from Service"); + + } + }; + + /** + * Called when connection to playback service has been established or + * information has to be refreshed + */ + private void queryService() { + Log.d(TAG, "Querying service info"); + if (playbackService != null) { + int requestedOrientation; + status = playbackService.getStatus(); + media = playbackService.getMedia(); + invalidateOptionsMenu(); + if (playbackService.isPlayingVideo()) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; @@ -523,17 +548,11 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements Log.d(TAG, "Orientation incorrect, waiting for orientation change"); } - - Log.d(TAG, "Connection to Service established"); - } - - @Override - public void onServiceDisconnected(ComponentName name) { - playbackService = null; - Log.d(TAG, "Disconnected from Service"); - + } else { + Log.e(TAG, + "queryService() was called without an existing connection to playbackservice"); } - }; + } private BroadcastReceiver statusUpdate = new BroadcastReceiver() { @Override @@ -565,17 +584,13 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements } break; case PlaybackService.NOTIFICATION_TYPE_RELOAD: - try { - unbindService(mConnection); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } if (positionObserver != null) { positionObserver.cancel(true); positionObserver = null; } mediaInfoLoaded = false; - bindToService(); + queryService(); + break; } @@ -587,81 +602,6 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements }; - /** Refreshes the current position of the media file that is playing. */ - public class MediaPositionObserver extends - AsyncTask<MediaPlayer, Void, Void> { - - private static final String TAG = "MediaPositionObserver"; - private static final int WAITING_INTERVALL = 1000; - private MediaPlayer player; - - @Override - protected void onCancelled() { - Log.d(TAG, "Task was cancelled"); - } - - @Override - protected Void doInBackground(MediaPlayer... p) { - Log.d(TAG, "Background Task started"); - player = p[0]; - try { - while (player.isPlaying() && !isCancelled()) { - try { - Thread.sleep(WAITING_INTERVALL); - } catch (InterruptedException e) { - Log.d(TAG, - "Thread was interrupted while waiting. Finishing now"); - return null; - } - publishProgress(); - - } - } catch (IllegalStateException e) { - Log.d(TAG, "player is in illegal state, exiting now"); - } - Log.d(TAG, "Background Task finished"); - return null; - } - } - - /** Hides the videocontrols after a certain period of time. */ - public class VideoControlsHider extends AsyncTask<Void, Void, Void> { - @Override - protected void onCancelled() { - videoControlsToggler = null; - } - - @Override - protected void onPostExecute(Void result) { - videoControlsToggler = null; - } - - private static final int WAITING_INTERVALL = 5000; - private static final String TAG = "VideoControlsToggler"; - - @Override - protected void onProgressUpdate(Void... values) { - if (videoControlsShowing) { - Log.d(TAG, "Hiding video controls"); - getSupportActionBar().hide(); - videoOverlay.setVisibility(View.GONE); - videoControlsShowing = false; - } - } - - @Override - protected Void doInBackground(Void... params) { - try { - Thread.sleep(WAITING_INTERVALL); - } catch (InterruptedException e) { - return null; - } - publishProgress(); - return null; - } - - } - private boolean holderCreated; @Override @@ -675,7 +615,12 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements holderCreated = true; Log.d(TAG, "Videoview holder created"); if (status == PlayerStatus.AWAITING_VIDEO_SURFACE) { - playbackService.setVideoSurface(holder); + if (playbackService != null) { + playbackService.setVideoSurface(holder); + } else { + Log.e(TAG, + "Could'nt attach surface to mediaplayer - reference to service was null"); + } } } @@ -685,7 +630,8 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements holderCreated = false; } - public static class MediaPlayerPagerAdapter extends FragmentPagerAdapter { + public static class MediaPlayerPagerAdapter extends + FragmentStatePagerAdapter { private int numItems; private MediaplayerActivity activity; @@ -744,4 +690,81 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements } + // ---------------------- ASYNC TASKS + + /** Refreshes the current position of the media file that is playing. */ + public class MediaPositionObserver extends + AsyncTask<MediaPlayer, Void, Void> { + + private static final String TAG = "MediaPositionObserver"; + private static final int WAITING_INTERVALL = 1000; + private MediaPlayer player; + + @Override + protected void onCancelled() { + Log.d(TAG, "Task was cancelled"); + } + + @Override + protected Void doInBackground(MediaPlayer... p) { + Log.d(TAG, "Background Task started"); + player = p[0]; + try { + while (player.isPlaying() && !isCancelled()) { + try { + Thread.sleep(WAITING_INTERVALL); + } catch (InterruptedException e) { + Log.d(TAG, + "Thread was interrupted while waiting. Finishing now"); + return null; + } + publishProgress(); + + } + } catch (IllegalStateException e) { + Log.d(TAG, "player is in illegal state, exiting now"); + } + Log.d(TAG, "Background Task finished"); + return null; + } + } + + /** Hides the videocontrols after a certain period of time. */ + public class VideoControlsHider extends AsyncTask<Void, Void, Void> { + @Override + protected void onCancelled() { + videoControlsToggler = null; + } + + @Override + protected void onPostExecute(Void result) { + videoControlsToggler = null; + } + + private static final int WAITING_INTERVALL = 5000; + private static final String TAG = "VideoControlsToggler"; + + @Override + protected void onProgressUpdate(Void... values) { + if (videoControlsShowing) { + Log.d(TAG, "Hiding video controls"); + getSupportActionBar().hide(); + videoOverlay.setVisibility(View.GONE); + videoControlsShowing = false; + } + } + + @Override + protected Void doInBackground(Void... params) { + try { + Thread.sleep(WAITING_INTERVALL); + } catch (InterruptedException e) { + return null; + } + publishProgress(); + return null; + } + + } + } diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 07193ce7f..6c011d8b0 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -90,7 +90,7 @@ public class PlaybackService extends Service { private PositionSaver positionSaver; private WidgetUpdateWorker widgetUpdater; - private PlayerStatus statusBeforeSeek; + private volatile PlayerStatus statusBeforeSeek; /** True if mediaplayer was paused because it lost audio focus temporarily */ private boolean pausedBecauseOfTransientAudiofocusLoss; |