diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-02-02 23:14:31 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-02-02 23:14:31 +0100 |
commit | 2d2e8ef627594a21c4a3da188f6624044a143f23 (patch) | |
tree | 7115def90decdf49102c8b972c9b820491fee350 | |
parent | 4b9831603b5f318494f59c33f99a04e0cf2477b0 (diff) | |
download | AntennaPod-2d2e8ef627594a21c4a3da188f6624044a143f23.zip |
Improved content view of audio player activity
-rw-r--r-- | res/layout/mediaplayer_activity.xml | 71 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/activity/AudioplayerActivity.java | 276 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/fragment/CoverFragment.java | 43 |
3 files changed, 263 insertions, 127 deletions
diff --git a/res/layout/mediaplayer_activity.xml b/res/layout/mediaplayer_activity.xml index d984aeb8f..221b5dff7 100644 --- a/res/layout/mediaplayer_activity.xml +++ b/res/layout/mediaplayer_activity.xml @@ -5,11 +5,66 @@ android:background="?attr/non_transparent_background" android:orientation="vertical" > - <com.viewpagerindicator.TabPageIndicator - android:id="@+id/tabs" + <RelativeLayout + android:id="@+id/navBar" android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:layout_alignParentTop="true" /> + android:layout_height="60dp" + android:layout_alignParentTop="true" > + + <ImageButton + android:id="@+id/butNavLeft" + android:layout_width="60dp" + android:layout_height="match_parent" + android:layout_alignParentLeft="true" + android:background="?attr/borderless_button" + android:src="?attr/default_cover" /> + + <ImageButton + android:id="@+id/butNavRight" + android:layout_width="60dp" + android:layout_height="match_parent" + android:layout_alignParentRight="true" + android:background="?attr/borderless_button" + android:src="?attr/default_cover" /> + + <TextView + android:id="@+id/txtvTitle" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentTop="true" + android:layout_marginLeft="8dp" + android:layout_marginRight="8dp" + android:layout_marginTop="8dp" + android:layout_toLeftOf="@id/butNavRight" + android:layout_toRightOf="@id/butNavLeft" + android:ellipsize="marquee" + android:marqueeRepeatLimit="marquee_forever" + android:maxLines="1" + android:textColor="?android:attr/textColorPrimary" + android:textSize="@dimen/text_size_medium" + android:textStyle="bold" /> + + <TextView + android:id="@+id/txtvFeed" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_below="@id/txtvTitle" + android:layout_marginLeft="8dp" + android:layout_marginRight="8dp" + android:layout_toLeftOf="@id/butNavRight" + android:layout_toRightOf="@id/butNavLeft" + android:ellipsize="marquee" + android:marqueeRepeatLimit="marquee_forever" + android:maxLines="1" + android:textColor="?android:attr/textColorSecondary" + android:textSize="@dimen/text_size_small" /> + </RelativeLayout> + + <View + android:layout_width="match_parent" + android:layout_height="1dp" + android:layout_below="@id/navBar" + android:background="@color/bright_blue" /> <RelativeLayout android:id="@+id/player_control" @@ -91,12 +146,12 @@ android:max="500" /> </RelativeLayout> - <android.support.v4.view.ViewPager - android:id="@+id/viewpager" + <FrameLayout + android:id="@+id/contentView" android:layout_width="match_parent" android:layout_height="0px" android:layout_above="@id/playtime_layout" - android:layout_below="@id/tabs" > - </android.support.v4.view.ViewPager> + android:layout_below="@id/navBar" > + </FrameLayout> </RelativeLayout>
\ No newline at end of file diff --git a/src/de/danoeh/antennapod/activity/AudioplayerActivity.java b/src/de/danoeh/antennapod/activity/AudioplayerActivity.java index ac1cf6ddb..33521ad65 100644 --- a/src/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -4,13 +4,17 @@ import android.content.Intent; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.View; +import android.view.View.OnClickListener; import android.widget.ArrayAdapter; +import android.widget.ImageButton; import android.widget.ListView; import android.widget.TextView; +import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.app.SherlockListFragment; import com.viewpagerindicator.TabPageIndicator; @@ -26,14 +30,48 @@ import de.danoeh.antennapod.service.PlaybackService; /** 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 Fragment[] detachedFragments; + private CoverFragment coverFragment; private ItemDescriptionFragment descriptionFragment; - ViewPager viewpager; - TabPageIndicator tabs; - MediaPlayerPagerAdapter pagerAdapter; + private SherlockListFragment chapterFragment; + + private Fragment currentlyShownFragment; + /** Fragment that was shown before the chapter fragment was displayed. */ + private int leftFragmentPosition = -1; + private int currentlyShownPosition = -1; + + private TextView txtvTitle; + private TextView txtvFeed; + private ImageButton butNavLeft; + private ImageButton butNavRight; + + public AudioplayerActivity() { + super(); + detachedFragments = new Fragment[NUM_CONTENT_FRAGMENTS]; + } + + private void resetFragmentView() { + currentlyShownFragment = null; + coverFragment = null; + descriptionFragment = null; + chapterFragment = null; + currentlyShownPosition = -1; + detachedFragments = new Fragment[NUM_CONTENT_FRAGMENTS]; + } + + @Override + protected void onPause() { + super.onPause(); + resetFragmentView(); + } @Override protected void onAwaitingVideoSurface() { @@ -50,20 +88,121 @@ public class AudioplayerActivity extends MediaplayerActivity { } + /** + * Changes the currently displayed fragment. + * + * @param 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) { + FeedMedia 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 + .getItem()); + } + currentlyShownFragment = coverFragment; + break; + case POS_DESCR: + if (descriptionFragment == null) { + descriptionFragment = ItemDescriptionFragment + .newInstance(media.getItem()); + } + currentlyShownFragment = descriptionFragment; + break; + case POS_CHAPTERS: + if (chapterFragment == null) { + chapterFragment = new SherlockListFragment() { + + @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.getItem() + .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.commit(); + } + } + } + } + @Override protected void setupGUI() { super.setupGUI(); - viewpager = (ViewPager) findViewById(R.id.viewpager); - tabs = (TabPageIndicator) findViewById(R.id.tabs); - pagerAdapter = new MediaPlayerPagerAdapter(getSupportFragmentManager()); - viewpager.setAdapter(pagerAdapter); - tabs.setViewPager(viewpager); + 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(leftFragmentPosition); + leftFragmentPosition = -1; + } + } + }); + + butNavRight.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + if (currentlyShownPosition == POS_CHAPTERS) { + switchToFragment(leftFragmentPosition); + leftFragmentPosition = -1; + } else { + leftFragmentPosition = currentlyShownPosition; + switchToFragment(POS_CHAPTERS); + } + }}); } @Override protected void onPositionObserverUpdate() { super.onPositionObserverUpdate(); - pagerAdapter.notifyMediaPositionChanged(); + notifyMediaPositionChanged(); } @Override @@ -71,114 +210,31 @@ public class AudioplayerActivity extends MediaplayerActivity { super.loadMediaInfo(); final FeedMedia media = controller.getMedia(); if (media != null) { - if (media.getItem().getChapters() != null - && pagerAdapter.getCount() < MediaPlayerPagerAdapter.NUM_ITEMS_WITH_CHAPTERS) { - pagerAdapter - .setNumItems(MediaPlayerPagerAdapter.NUM_ITEMS_WITH_CHAPTERS); - } - pagerAdapter.notifyDataSetChanged(); - } - } - - public class MediaPlayerPagerAdapter extends FragmentStatePagerAdapter { - private int numItems; - - private SherlockListFragment sCChapterFragment; - - private static final int POS_COVER = 0; - private static final int POS_DESCR = 1; - private static final int POS_CHAPTERS = 2; - - public static final int NUM_ITEMS_WITH_CHAPTERS = 3; - public static final int NUM_ITEMS_WITHOUT_CHAPTERS = 2; - - public MediaPlayerPagerAdapter(FragmentManager fm) { - super(fm); - numItems = NUM_ITEMS_WITHOUT_CHAPTERS; - FeedMedia media = AudioplayerActivity.this.controller.getMedia(); - if (media != null && media.getItem().getChapters() != null) { - numItems = NUM_ITEMS_WITH_CHAPTERS; - } - } - - @Override - public Fragment getItem(int position) { - FeedMedia media = controller.getMedia(); - if (media != null) { - switch (position) { - case POS_COVER: - AudioplayerActivity.this.coverFragment = CoverFragment - .newInstance(media.getItem()); - return AudioplayerActivity.this.coverFragment; - case POS_DESCR: - AudioplayerActivity.this.descriptionFragment = ItemDescriptionFragment - .newInstance(media.getItem()); - return AudioplayerActivity.this.descriptionFragment; - case POS_CHAPTERS: - sCChapterFragment = new SherlockListFragment() { - - @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); - } - - }; - - sCChapterFragment.setListAdapter(new ChapterListAdapter( - AudioplayerActivity.this, 0, media.getItem() - .getChapters(), media)); - - return sCChapterFragment; - default: - return CoverFragment.newInstance(null); - } + txtvTitle.setText(media.getItem().getTitle()); + txtvFeed.setText(media.getItem().getFeed().getTitle()); + if (media.getItem().getChapters() != null) { + butNavRight.setVisibility(View.VISIBLE); } else { - return CoverFragment.newInstance(null); + butNavRight.setVisibility(View.GONE); } - } - @Override - public CharSequence getPageTitle(int position) { - switch (position) { - case POS_COVER: - return AudioplayerActivity.this.getString(R.string.cover_label); - case POS_DESCR: - return AudioplayerActivity.this - .getString(R.string.shownotes_label); - case POS_CHAPTERS: - return AudioplayerActivity.this - .getString(R.string.chapters_label); - default: - return super.getPageTitle(position); - } } - - @Override - public int getCount() { - return numItems; + if (currentlyShownPosition == -1) { + switchToFragment(POS_COVER); } - - public void setNumItems(int numItems) { - this.numItems = numItems; + if (currentlyShownFragment instanceof AudioplayerContentFragment) { + ((AudioplayerContentFragment) currentlyShownFragment) + .onDataSetChanged(media); } - @Override - public int getItemPosition(Object object) { - return POSITION_UNCHANGED; - } + } - public void notifyMediaPositionChanged() { - if (sCChapterFragment != null) { - ArrayAdapter<SimpleChapter> adapter = (ArrayAdapter<SimpleChapter>) sCChapterFragment - .getListAdapter(); - adapter.notifyDataSetChanged(); - } + public void notifyMediaPositionChanged() { + if (chapterFragment != null) { + ArrayAdapter<SimpleChapter> adapter = (ArrayAdapter<SimpleChapter>) chapterFragment + .getListAdapter(); + adapter.notifyDataSetChanged(); } - } @Override @@ -202,4 +258,8 @@ public class AudioplayerActivity extends MediaplayerActivity { clearStatusMsg(); } + public interface AudioplayerContentFragment { + public void onDataSetChanged(FeedMedia media); + } + } diff --git a/src/de/danoeh/antennapod/fragment/CoverFragment.java b/src/de/danoeh/antennapod/fragment/CoverFragment.java index 945495670..e324a2928 100644 --- a/src/de/danoeh/antennapod/fragment/CoverFragment.java +++ b/src/de/danoeh/antennapod/fragment/CoverFragment.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.fragment; import android.os.Bundle; +import android.provider.MediaStore.Audio.AudioColumns; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -12,6 +13,7 @@ import com.actionbarsherlock.app.SherlockFragment; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.AudioplayerActivity.AudioplayerContentFragment; import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.FeedItem; @@ -19,7 +21,8 @@ import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedMedia; /** Displays the cover and the title of a FeedItem. */ -public class CoverFragment extends SherlockFragment { +public class CoverFragment extends SherlockFragment implements + AudioplayerContentFragment { private static final String TAG = "CoverFragment"; private static final String ARG_FEED_ID = "arg.feedId"; private static final String ARG_FEEDITEM_ID = "arg.feedItem"; @@ -30,6 +33,8 @@ public class CoverFragment extends SherlockFragment { private TextView txtvFeed; private ImageView imgvCover; + private boolean viewCreated = false; + public static CoverFragment newInstance(FeedItem item) { CoverFragment f = new CoverFragment(); if (item != null) { @@ -44,7 +49,7 @@ public class CoverFragment extends SherlockFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + setRetainInstance(true); FeedManager manager = FeedManager.getInstance(); FeedItem item = null; Bundle args = getArguments(); @@ -70,25 +75,32 @@ public class CoverFragment extends SherlockFragment { txtvTitle = (TextView) root.findViewById(R.id.txtvTitle); txtvFeed = (TextView) root.findViewById(R.id.txtvFeed); imgvCover = (ImageView) root.findViewById(R.id.imgvCover); + viewCreated = true; return root; } private void loadMediaInfo() { - imgvCover.post(new Runnable() { + if (media != null) { + imgvCover.post(new Runnable() { - @Override - public void run() { - FeedImageLoader.getInstance().loadCoverBitmap( - media.getItem().getFeed().getImage(), imgvCover); - } - }); + @Override + public void run() { + FeedImageLoader.getInstance().loadCoverBitmap( + media.getItem().getFeed().getImage(), imgvCover); + } + }); - txtvTitle.setText(media.getItem().getTitle()); - txtvFeed.setText(media.getItem().getFeed().getTitle()); + txtvTitle.setText(media.getItem().getTitle()); + txtvFeed.setText(media.getItem().getFeed().getTitle()); + } else { + Log.w(TAG, "loadMediaInfo was called while media was null"); + } } @Override public void onStart() { + if (AppConfig.DEBUG) + Log.d(TAG, "On Start"); super.onStart(); if (media != null) { if (AppConfig.DEBUG) @@ -99,4 +111,13 @@ public class CoverFragment extends SherlockFragment { } } + @Override + public void onDataSetChanged(FeedMedia media) { + this.media = media; + if (viewCreated) { + loadMediaInfo(); + } + + } + } |