From 3932e09089032fc91ce2d361443e5c3df5de6b41 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Sun, 1 Jul 2012 18:32:52 +0200 Subject: [PATCH] Added viewpager to mediaplayeractivity --- AndroidManifest.xml | 2 +- res/layout/cover_fragment.xml | 48 +++++++++ res/layout/mediaplayer_activity.xml | 52 ++++----- res/values/strings.xml | 1 + .../activity/MediaplayerActivity.java | 102 +++++++++++++++--- src/de/podfetcher/fragment/CoverFragment.java | 89 +++++++++++++++ 6 files changed, 245 insertions(+), 49 deletions(-) create mode 100644 res/layout/cover_fragment.xml create mode 100644 src/de/podfetcher/fragment/CoverFragment.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9d296eb81..8c4ec9157 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -30,7 +30,7 @@ - + diff --git a/res/layout/cover_fragment.xml b/res/layout/cover_fragment.xml new file mode 100644 index 000000000..c7f60f2da --- /dev/null +++ b/res/layout/cover_fragment.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/mediaplayer_activity.xml b/res/layout/mediaplayer_activity.xml index effe3ab57..976ccabaa 100644 --- a/res/layout/mediaplayer_activity.xml +++ b/res/layout/mediaplayer_activity.xml @@ -4,16 +4,11 @@ android:layout_height="match_parent" android:orientation="vertical" > - - + android:layout_alignParentTop="true" /> + android:src="@drawable/horizontal_divider" /> + + + - - - - - - - + android:src="@drawable/horizontal_divider" /> \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 1c8f382a1..f86a755b4 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -68,5 +68,6 @@ An error occured: Connection error Type in the URL of the Feed here: + Cover \ No newline at end of file diff --git a/src/de/podfetcher/activity/MediaplayerActivity.java b/src/de/podfetcher/activity/MediaplayerActivity.java index 38f168632..5e9f90aa5 100644 --- a/src/de/podfetcher/activity/MediaplayerActivity.java +++ b/src/de/podfetcher/activity/MediaplayerActivity.java @@ -14,6 +14,11 @@ import android.media.MediaPlayer; import android.os.AsyncTask; import android.os.Bundle; 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.SurfaceHolder; import android.view.SurfaceView; @@ -28,17 +33,21 @@ import android.widget.VideoView; import android.widget.ViewSwitcher; import com.actionbarsherlock.app.SherlockActivity; +import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; +import com.viewpagerindicator.TabPageIndicator; import de.podfetcher.PodcastApp; import de.podfetcher.R; import de.podfetcher.feed.FeedManager; import de.podfetcher.feed.FeedMedia; +import de.podfetcher.fragment.CoverFragment; +import de.podfetcher.fragment.ItemDescriptionFragment; import de.podfetcher.service.PlaybackService; import de.podfetcher.service.PlayerStatus; import de.podfetcher.util.Converter; -public class MediaplayerActivity extends SherlockActivity implements +public class MediaplayerActivity extends SherlockFragmentActivity implements SurfaceHolder.Callback { private final String TAG = "MediaplayerActivity"; @@ -57,9 +66,11 @@ public class MediaplayerActivity extends SherlockActivity implements private FeedManager manager; // Widgets - private TextView txtvTitle; - private TextView txtvFeed; - private ImageView imgvCover; + private CoverFragment coverFragment; + private ItemDescriptionFragment descriptionFragment; + private ViewPager viewpager; + private TabPageIndicator tabs; + private MediaPlayerPagerAdapter pagerAdapter; private VideoView videoview; private TextView txtvStatus; private TextView txtvPosition; @@ -200,7 +211,6 @@ public class MediaplayerActivity extends SherlockActivity implements break; case STOPPED: setStatusMsg(R.string.player_stopped_msg, View.VISIBLE); - imgvCover.setImageBitmap(null); break; case PREPARED: loadMediaInfo(); @@ -261,10 +271,8 @@ public class MediaplayerActivity extends SherlockActivity implements getSupportActionBar().setSubtitle(media.getItem().getTitle()); getSupportActionBar().setTitle( media.getItem().getFeed().getTitle()); - imgvCover.setImageBitmap(media.getItem().getFeed().getImage() - .getImageBitmap()); - txtvTitle.setText(media.getItem().getTitle()); - txtvFeed.setText(media.getItem().getFeed().getTitle()); + pagerAdapter.notifyDataSetChanged(); + } txtvPosition.setText(Converter.getDurationStringLong((player @@ -287,6 +295,8 @@ public class MediaplayerActivity extends SherlockActivity implements butRev = (ImageButton) findViewById(R.id.butRev); butFF = (ImageButton) findViewById(R.id.butFF); + // SEEKBAR SETUP + sbPosition.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { int duration; float prog; @@ -319,6 +329,8 @@ public class MediaplayerActivity extends SherlockActivity implements } }); + // BUTTON SETUP + butPlay.setOnClickListener(playbuttonListener); butFF.setOnClickListener(new OnClickListener() { @@ -339,12 +351,16 @@ public class MediaplayerActivity extends SherlockActivity implements } }); - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - imgvCover = (ImageView) findViewById(R.id.imgvCover); - txtvStatus = (TextView) findViewById(R.id.txtvStatus); - txtvTitle = (TextView) findViewById(R.id.txtvTitle); - txtvFeed = (TextView) findViewById(R.id.txtvFeed); + // PORTRAIT ORIENTATION SETUP + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + txtvStatus = (TextView) findViewById(R.id.txtvStatus); + viewpager = (ViewPager) findViewById(R.id.viewpager); + tabs = (TabPageIndicator) findViewById(R.id.tabs); + pagerAdapter = new MediaPlayerPagerAdapter( + getSupportFragmentManager(), 2, this); + viewpager.setAdapter(pagerAdapter); + tabs.setViewPager(viewpager); } else { setTheme(R.style.Theme_Sherlock_Light_NoActionBar); videoview = (VideoView) findViewById(R.id.videoview); @@ -458,4 +474,62 @@ public class MediaplayerActivity extends SherlockActivity implements holderCreated = false; } + public static class MediaPlayerPagerAdapter extends FragmentStatePagerAdapter { + private int numItems; + private MediaplayerActivity activity; + + private static final int POS_COVER = 0; + private static final int POS_DESCR = 1; + private static final int POS_CHAPTERS = 2; + + public MediaPlayerPagerAdapter(FragmentManager fm, int numItems, + MediaplayerActivity activity) { + super(fm); + this.numItems = numItems; + this.activity = activity; + } + + @Override + public Fragment getItem(int position) { + if (activity.media != null) { + switch (position) { + case POS_COVER: + activity.coverFragment = CoverFragment.newInstance(activity.media.getItem()); + return activity.coverFragment; + case POS_DESCR: + activity.descriptionFragment = ItemDescriptionFragment + .newInstance(activity.media.getItem()); + return activity.descriptionFragment; + default: + return CoverFragment.newInstance(null); + } + } else { + return CoverFragment.newInstance(null); + } + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case POS_COVER: + return activity.getString(R.string.cover_label); + case POS_DESCR: + return activity.getString(R.string.description_label); + default: + return super.getPageTitle(position); + } + } + + @Override + public int getCount() { + return numItems; + } + + @Override + public int getItemPosition(Object object) { + return POSITION_NONE; + } + + } + } diff --git a/src/de/podfetcher/fragment/CoverFragment.java b/src/de/podfetcher/fragment/CoverFragment.java new file mode 100644 index 000000000..81a42dcec --- /dev/null +++ b/src/de/podfetcher/fragment/CoverFragment.java @@ -0,0 +1,89 @@ +package de.podfetcher.fragment; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.actionbarsherlock.app.SherlockFragment; + +import de.podfetcher.R; +import de.podfetcher.feed.Feed; +import de.podfetcher.feed.FeedItem; +import de.podfetcher.feed.FeedManager; +import de.podfetcher.feed.FeedMedia; + +/** Displays the cover and the title of a FeedItem. */ +public class CoverFragment extends SherlockFragment { + private static final String TAG = "CoverFragment"; + private static final String ARG_FEED_ID = "arg.feedId"; + private static final String ARG_FEEDITEM_ID = "arg.feedItem"; + + private FeedMedia media; + + private TextView txtvTitle; + private TextView txtvFeed; + private ImageView imgvCover; + + public static CoverFragment newInstance(FeedItem item) { + CoverFragment f = new CoverFragment(); + if (item != null) { + Bundle args = new Bundle(); + args.putLong(ARG_FEED_ID, item.getFeed().getId()); + args.putLong(ARG_FEEDITEM_ID, item.getId()); + f.setArguments(args); + } + return f; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + FeedManager manager = FeedManager.getInstance(); + FeedItem item = null; + Bundle args = getArguments(); + if (args != null) { + long feedId = args.getLong(ARG_FEED_ID, -1); + long itemId = args.getLong(ARG_FEEDITEM_ID, -1); + if (feedId != -1 && itemId != -1) { + Feed feed = manager.getFeed(feedId); + item = manager.getFeedItem(itemId, feed); + media = item.getMedia(); + } else { + Log.e(TAG, TAG + " was called with invalid arguments"); + } + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View root = inflater.inflate(R.layout.cover_fragment, container, false); + txtvTitle = (TextView) root.findViewById(R.id.txtvTitle); + txtvFeed = (TextView) root.findViewById(R.id.txtvFeed); + imgvCover = (ImageView) root.findViewById(R.id.imgvCover); + return root; + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (media != null) { + loadMediaInfo(); + } else { + Log.w(TAG, "Unable to load media info: media was null"); + } + } + + private void loadMediaInfo() { + imgvCover.setImageBitmap(media.getItem().getFeed().getImage() + .getImageBitmap()); + txtvTitle.setText(media.getItem().getTitle()); + txtvFeed.setText(media.getItem().getFeed().getTitle()); + } + +}