[Android] src: Distinctly separate directory and media library mode

Edward Wang git at videolan.org
Sat Jun 23 06:13:19 CEST 2012


android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Fri Jun 22 19:54:33 2012 -0400| [78e6020171bf79e477f3e68fe967287ed901d3ae] | committer: Edward Wang

src: Distinctly separate directory and media library mode

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=78e6020171bf79e477f3e68fe967287ed901d3ae
---

 vlc-android/res/layout/directory_view.xml          |    3 +-
 .../src/org/videolan/vlc/gui/MainActivity.java     |   92 ++++++++++++++------
 2 files changed, 68 insertions(+), 27 deletions(-)

diff --git a/vlc-android/res/layout/directory_view.xml b/vlc-android/res/layout/directory_view.xml
index 98f75a1..4c8b6da 100644
--- a/vlc-android/res/layout/directory_view.xml
+++ b/vlc-android/res/layout/directory_view.xml
@@ -4,6 +4,7 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:divider="@color/item_common"
-    android:dividerHeight="1dp">
+    android:dividerHeight="1dp"
+    android:background="@android:color/black">
 
 </ListView>
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index d6e570f..9435a10 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -87,6 +87,9 @@ public class MainActivity extends SherlockFragmentActivity {
     private View mInfoLayout;
     private ProgressBar mInfoProgress;
     private TextView mInfoText;
+    private DirectoryViewFragment mDirectoryView;
+    private int mCurrentViewTab;
+    private Boolean mMediaLibraryActive;
 
     private SharedPreferences mSettings;
 
@@ -114,22 +117,23 @@ public class MainActivity extends SherlockFragmentActivity {
         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
         mActionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
 
-        mActionBar.addTab(mActionBar.newTab()
-                .setText("Video")
-                .setIcon(R.drawable.header_icon_video)
-                .setTabListener(new TabListener<VideoListFragment>(
-                        this, "video", VideoListFragment.class)));
-
-        mActionBar.addTab(mActionBar.newTab()
-                .setText("Audio")
-                .setIcon(R.drawable.header_icon_audio)
-                .setTabListener(new TabListener<AudioBrowserFragment>(
-                        this, "audio", AudioBrowserFragment.class)));
-
+        int tabToShow = 0;
         if (savedInstanceState != null) {
-            mActionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
+            tabToShow = savedInstanceState.getInt("tab", 0);
         }
 
+        addMediaLibraryTabs(tabToShow);
+        mCurrentViewTab = tabToShow;
+        mMediaLibraryActive = true;
+        mDirectoryView = new DirectoryViewFragment(this);
+        mDirectoryView.setRetainInstance(true); /* Retain instance across attach/detach */
+        getSupportFragmentManager().beginTransaction()
+            .add(R.id.fragment_placeholder, mDirectoryView)
+            .commit();
+        getSupportFragmentManager().beginTransaction()
+            .detach(mDirectoryView)
+            .commit();
+
         // Add mini audio player
         mAudioPlayer = (AudioMiniPlayer) findViewById(R.id.audio_mini_player);
         mAudioController = AudioServiceController.getInstance();
@@ -141,7 +145,7 @@ public class MainActivity extends SherlockFragmentActivity {
             Log.d(TAG, "Started from notification.");
             showAudioTab();
         } else {
-            // load the last tab-state
+            // load the last tab-state (TODO: Broken)
             int state = savedInstanceState == null ? VIDEO_TAB : savedInstanceState.getInt("mCurrentState");
             if(state == VIDEO_TAB)
                 showVideoTab();
@@ -173,6 +177,22 @@ public class MainActivity extends SherlockFragmentActivity {
         MediaLibrary.getInstance(this).loadMediaItems(this);
     }
 
+    private void addMediaLibraryTabs(int tabToShow) {
+        mActionBar.addTab(mActionBar.newTab()
+                .setText("Video")
+                .setIcon(R.drawable.header_icon_video)
+                .setTabListener(new TabListener<VideoListFragment>(
+                        this, "video", VideoListFragment.class)));
+
+        mActionBar.addTab(mActionBar.newTab()
+                .setText("Audio")
+                .setIcon(R.drawable.header_icon_audio)
+                .setTabListener(new TabListener<AudioBrowserFragment>(
+                        this, "audio", AudioBrowserFragment.class)));
+
+        mActionBar.setSelectedNavigationItem(tabToShow);
+    }
+
     @Override
     protected void onResume() {
         mAudioController.addAudioPlayer(mAudioPlayer);
@@ -254,10 +274,7 @@ public class MainActivity extends SherlockFragmentActivity {
                 break;
             // Browse Folders
             case R.id.ml_menu_browse:
-                FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
-                ft.replace(R.id.fragment_placeholder, new DirectoryViewFragment(this));
-                ft.addToBackStack(null);
-                ft.commit();
+                showDirectoryView();
                 break;
             // Open MRL
             case R.id.ml_menu_open_mrl:
@@ -328,6 +345,31 @@ public class MainActivity extends SherlockFragmentActivity {
         return super.onOptionsItemSelected(item);
     }
 
+    private void showDirectoryView() {
+        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
+        if(!mMediaLibraryActive) {
+            /* Animate based on which tab is about to be loaded
+             * Video comes in on left so exit right
+             * Audio comes in on right so exit left */
+            if(mCurrentViewTab == AUDIO_TAB)
+                ft.setCustomAnimations(R.anim.anim_enter_left, R.anim.anim_leave_left);
+            else if(mCurrentViewTab == VIDEO_TAB)
+                ft.setCustomAnimations(R.anim.anim_enter_right, R.anim.anim_leave_right);
+            /* Remove the directory view from the tabs */
+            ft.detach(mDirectoryView);
+            ft.commit();
+            /* Restore the tabs */
+            addMediaLibraryTabs(mCurrentViewTab);
+        } else {
+            /* Remove existing tabs */
+            mActionBar.removeAllTabs();
+            /* Load directory view fragment */
+            ft.attach(mDirectoryView);
+            ft.commit();
+        }
+        this.mMediaLibraryActive = !this.mMediaLibraryActive;
+    }
+
     /*@Override
     public boolean onContextItemSelected(MenuItem item) {
         switch (item.getItemId()) {
@@ -462,12 +504,6 @@ public class MainActivity extends SherlockFragmentActivity {
         context.getApplicationContext().sendBroadcast(intent);
     }
 
-
-
-
-
-
-
     public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
         private final SherlockFragmentActivity mActivity;
         private final String mTag;
@@ -498,13 +534,17 @@ public class MainActivity extends SherlockFragmentActivity {
 
         @Override
         public void onTabSelected(Tab tab, FragmentTransaction ft) {
-	        if (mTag.equalsIgnoreCase("video"))
+	        if (mTag.equalsIgnoreCase("video")) {
 	            ft.setCustomAnimations((mFragment == null) ? 0 : R.anim.anim_enter_left, R.anim.anim_leave_left);
-	        else if (mTag.equalsIgnoreCase("audio"))
+	            ((MainActivity)mActivity).mCurrentViewTab = VIDEO_TAB;
+	        } else if (mTag.equalsIgnoreCase("audio")) {
 	            ft.setCustomAnimations(R.anim.anim_enter_right, R.anim.anim_leave_right);
+	            ((MainActivity)mActivity).mCurrentViewTab = AUDIO_TAB;
+	        }
 
             if (mFragment == null) {
                 mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
+                mFragment.setRetainInstance(true);
                 ft.add(R.id.fragment_placeholder, mFragment, mTag);
             } else {
                 ft.attach(mFragment);



More information about the Android mailing list