Browse Source

Merge pull request #985 from nextcloud/audioRotationBug

Re-use second fragment if available
Mario Đanić 8 years ago
parent
commit
474314b2dc

+ 4 - 0
src/main/java/com/owncloud/android/media/MediaControlView.java

@@ -197,6 +197,10 @@ public class MediaControlView extends FrameLayout /* implements OnLayoutChangeLi
         updatePausePlay();
     }
 
+    public void stopMediaPlayerMessages() {
+        mHandler.removeMessages(SHOW_PROGRESS);
+    }
+
 
     private void initControllerView(View v) {
         mPauseButton = (ImageButton) v.findViewById(R.id.playBtn);

+ 10 - 10
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -431,7 +431,12 @@ public class FileDisplayActivity extends HookActivity
 
             /// Second fragment
             OCFile file = getFile();
-            Fragment secondFragment = chooseInitialSecondFragment(file);
+
+            Fragment secondFragment = getSecondFragment();
+            if (secondFragment == null) {
+                secondFragment = chooseInitialSecondFragment(file);
+            }
+
             if (secondFragment != null) {
                 setSecondFragment(secondFragment);
                 updateFragmentsVisibility(true);
@@ -470,13 +475,9 @@ public class FileDisplayActivity extends HookActivity
         Fragment secondFragment = null;
         if (file != null && !file.isFolder()) {
             if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)) {
-                int startPlaybackPosition =
-                        getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
-                boolean autoplay =
-                        getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
-                secondFragment = new PreviewMediaFragment(file, getAccount(),
-                        startPlaybackPosition, autoplay);
-
+                int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
+                boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
+                secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay);
             } else if (file.isDown() && PreviewTextFragment.canBePreviewed(file)) {
                 secondFragment = null;
             } else {
@@ -550,8 +551,7 @@ public class FileDisplayActivity extends HookActivity
     }
 
     public FileFragment getSecondFragment() {
-        Fragment second = getSupportFragmentManager().findFragmentByTag(
-                FileDisplayActivity.TAG_SECOND_FRAGMENT);
+        Fragment second = getSupportFragmentManager().findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT);
         if (second != null) {
             return (FileFragment) second;
         }

+ 2 - 2
src/main/java/com/owncloud/android/ui/fragment/ExtendedListFragment.java

@@ -282,8 +282,8 @@ public class ExtendedListFragment extends Fragment
     }
 
     public boolean onQueryTextChange(final String query) {
-        if (getFragmentManager().findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT)
-                instanceof ExtendedListFragment){
+        if (getFragmentManager() != null && getFragmentManager().
+                findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT) instanceof ExtendedListFragment) {
             performSearch(query, false);
             return true;
         } else {

+ 14 - 16
src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -106,6 +106,7 @@ public class PreviewMediaFragment extends FileFragment implements
     private MediaServiceConnection mMediaServiceConnection = null;
     private VideoHelper mVideoHelper;
     private boolean mAutoplay;
+    private static boolean mOnResume = false;
     public boolean mPrepared;
 
     private static final String TAG = PreviewMediaFragment.class.getSimpleName();
@@ -220,6 +221,7 @@ public class PreviewMediaFragment extends FileFragment implements
      */
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
+        mOnResume = true;
         super.onActivityCreated(savedInstanceState);
         Log_OC.v(TAG, "onActivityCreated");
 
@@ -234,14 +236,11 @@ public class PreviewMediaFragment extends FileFragment implements
             if (!file.isDown()) {
                 throw new IllegalStateException("There is no local file to preview");
             }
-
-        }
-        else {
-            file = (OCFile) savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
+        } else {
+            file = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
             setFile(file);
             mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
-            mSavedPlaybackPosition =
-                    savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
+            mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
             mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
 
         }
@@ -251,15 +250,12 @@ public class PreviewMediaFragment extends FileFragment implements
                 mVideoPreview.setVisibility(View.VISIBLE);
                 mImagePreview.setVisibility(View.GONE);
                 prepareVideo();
-
-            }
-            else {
+            } else {
                 mVideoPreview.setVisibility(View.GONE);
                 mImagePreview.setVisibility(View.VISIBLE);
                 extractAndSetCoverArt(file);
             }
         }
-
     }
 
     /**
@@ -307,11 +303,8 @@ public class PreviewMediaFragment extends FileFragment implements
         }
         else {
             if (mMediaServiceBinder != null) {
-                outState.putInt(
-                        PreviewMediaFragment.EXTRA_PLAY_POSITION,
-                        mMediaServiceBinder.getCurrentPosition());
-                outState.putBoolean(
-                        PreviewMediaFragment.EXTRA_PLAYING, mMediaServiceBinder.isPlaying());
+                outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mMediaServiceBinder.getCurrentPosition());
+                outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mMediaServiceBinder.isPlaying());
             }
         }
     }
@@ -565,6 +558,8 @@ public class PreviewMediaFragment extends FileFragment implements
     @Override
     public void onResume() {
         super.onResume();
+        mOnResume = !mOnResume;
+
         if (getActivity() != null) {
             AnalyticsUtils.setCurrentScreenName(getActivity(), SCREEN_NAME, TAG);
         }
@@ -585,6 +580,7 @@ public class PreviewMediaFragment extends FileFragment implements
         if (mMediaServiceConnection != null) {
             Log_OC.d(TAG, "Unbinding from MediaService ...");
             if (mMediaServiceBinder != null && mMediaController != null) {
+                mMediaController.stopMediaPlayerMessages();
                 mMediaServiceBinder.unregisterMediaController(mMediaController);
             }
             getActivity().unbindService(mMediaServiceConnection);
@@ -638,7 +634,7 @@ public class PreviewMediaFragment extends FileFragment implements
 
     private void playAudio() {
         OCFile file = getFile();
-        if (!mMediaServiceBinder.isPlaying(file)) {
+        if (!mMediaServiceBinder.isPlaying(file) && !mOnResume) {
             Log_OC.d(TAG, "starting playback of " + file.getStoragePath());
             mMediaServiceBinder.start(mAccount, file, mAutoplay, mSavedPlaybackPosition);
 
@@ -649,6 +645,8 @@ public class PreviewMediaFragment extends FileFragment implements
                 mMediaController.updatePausePlay();
             }
         }
+
+        mOnResume = false;
     }